简体   繁体   English

应用终止时的远程通知

[英]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.

相关问题 当应用未运行(终止)时,通知应用有关远程推送通知的信息 - Notify the app about remote push notification when the app is not running (terminated) 应用终止时处理远程推送通知的声音 - Handling remote push notification sounds when app is terminated 应用程序终止时推送通知 - Push notification when app is terminated 即使应用程序处于后台或终止状态,我该如何采取措施接收远程通知? - How can I do an action on receiving a remote notification even when the app is in background or terminated? 应用终止时响应推送通知 - responding to push notification when app is TERMINATED 在应用终止时安排本地通知 - Scheduling a local notification when the app is terminated 应用程序终止时的 Swift IOS 启动通知 - Swift IOS launch notification when app terminated 当应用程序在后台/终止时如何忽略远程/推送通知 - How to ignore a remote/push notification while app is on background/terminated 应用未运行/应用终止时如何处理推送通知 - How to handle push notification when app is not running/app is terminated 应用未运行/应用终止时如何获取推送通知 - How to get push notification when app is not running/app is terminated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM