[英]Remote notification when app is terminated
I am new to iOS and Swift. 我是iOS和Swift的新手。 I am implementing remote notification in my app.
我正在我的应用程序中实现远程通知。 Everything works fine when the app is active or in the background.
当应用处于活动状态或在后台运行时,一切正常。 But my notification does not show up when the app is terminated.
但是当应用终止时,我的通知没有显示。 All I am getting is the alert sound of the notification.
我得到的只是通知的警报声音。
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)
}
}
Below AppDelegate method will be called When you receive a notification and application is in killed state 当您收到通知并且应用程序处于终止状态时,将调用下面的AppDelegate方法
didFinishLaunchingWithOptions
So you should handle it properly from here when app is in killed state. 因此,当应用处于终止状态时,您应该从此处正确处理它。
Below code helps to identify the Remote/push or Local notification in didFinishLaunchingWithOptions method: 以下代码有助于在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.