0

我是 iOS 和 Swift 的新手。我正在我的应用程序中实现远程通知。当应用程序处于活动状态或在后台时,一切正常。但是当应用程序终止时,我的通知没有出现。我得到的只是通知的警报声。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    if #available(iOS 10.0, *){
    }else{
        let notification = UILocalNotification()
        notification.alertTitle = "my Title"
        notification.alertBody = "My Message"
        notification.category = "customCategory"
        notification.soundName = UILocalNotificationDefaultSoundName
        application.scheduleLocalNotification(notification)
    }
}
4

1 回答 1

-2

当您收到通知并且应用程序处于终止状态时,将调用下面的 AppDelegate 方法

didFinishLaunchingWithOptions

因此,当应用程序处于终止状态时,您应该从这里正确处理它。

下面的代码有助于识别 didFinishLaunchingWithOptions 方法中的远程/推送或本地通知:

if let launchOpts = launchOptions as [UIApplicationLaunchOptionsKey: Any]? {
            if let notificationPayload = launchOpts[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {

                 //Handle push notification here
                }
else if let notification = (launchOpts as NSDictionary).object(forKey: "UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
               //Handle local notification here
                }
于 2017-01-03T17:33:10.940 回答