![](/img/trans.png)
[英]Notify the app about remote push notification when the app is not running (terminated)
[英]Remote notification when app is terminated
我是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)
}
}
当您收到通知并且应用程序处于终止状态时,将调用下面的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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.