我是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)
}
}
答案 0 :(得分:-2)
当您收到通知并且应用程序处于被杀死状态时,将调用AppDelegate方法
didFinishLaunchingWithOptions
因此,当app处于被杀死状态时,你应该从这里正确处理它。
下面的代码有助于识别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
}