0

我正在尝试在我正在构建的应用程序上实现本地通知。我的大部分代码都遵循文档中所写的内容

我将在下面发布我的代码。我目前的问题是通知永远不会出现。我第一次加载应用程序时出现了权限屏幕,我说“允许”

在 AppDelegate 中的 didFinishLaunchingWithOptions 方法中

didFinishLaunchingWithOptions

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
    [self setupNotification];
}];

以下也在 AppDelegate 中

-(void)setupNotification {
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = [NSString localizedUserNotificationStringForKey:@"New:" arguments:nil];
        content.body = [NSString localizedUserNotificationStringForKey:@"New Notification"
                                                             arguments:nil];

        content.sound = [UNNotificationSound defaultSound];


        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
                                                      triggerWithTimeInterval:5
                                                      repeats:NO];


        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NOTIFICATION"
                                                                              content:content
                                                                              trigger:trigger];


        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];


        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if (!error) {
                NSLog(@"success");
            }
        }];
    }

正如我之前所说,通知从未出现,我不知道为什么。我设置了内容、触发器和请求,然后将请求添加到 UNUserNotificationCenter。

有没有人有一个可行的例子,或者可以告诉我哪里出错了?

我在这里找到了一个类似的答案,但这个答案没有解决为什么UNTimeIntervalNotificationTrigger不起作用,而是解释了如何设置UNCalendarNotificationTrigger

谢谢

4

1 回答 1

0

UNUserNotificationCenter Swift 3 实现和使用:

//  AppDelegate.swift

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    // Override point for customization after application launch.
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Setup Notifications
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (bool, error) in

        }

        return true
    }

创建一个“Extension.swift”文件,并添加:

//  Extensions.swift

import UIKit
import UserNotifications

extension UNUserNotificationCenter {

    func scheduleNotification(identifier: String, body: String, time: Double) {
        let content =  UNMutableNotificationContent()
        content.body = body
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request) { (error) in
            if (error != nil) {
                print(error!)
            } else {
                print("Success! ID:\(identifier), Message:\(body), Time:\(trigger.timeInterval.magnitude) seconds")
            }
        }
    }
}

在任何视图控制器中,在“类”(viewDidLoad 上方)下声明:

var notificationCenter: UNUserNotificationCenter?

在“viewDidLoad”函数中,初始化UNUserNotificationCenter:

notificationCenter = UNUserNotificationCenter.current()

从视图控制器中的任何函数调用通知:

// Use any identifier, Message and time (in seconds)
self.notificationCenter?.scheduleNotification(identifier: "welcomeScreen", body: "Welcome to my App!", time:20)

这应该在 Swift 3 和 iOS 10 中启用本地通知。

视频参考:https ://youtu.be/Svul_gCtzck

于 2017-05-02T23:25:55.903 回答