Don't show notification when in foreground

前端 未结 2 876
走了就别回头了
走了就别回头了 2021-01-14 19:08

I\'m sending push notification from my node server using the Firebase admin SDK. But on iOS I only want to show the notification when the app is in the background/terminated

2条回答
  •  长情又很酷
    2021-01-14 19:19

    In iOS you decide in AppDelegate.swift, what to do with notifications. In AppDelegate handle the notification for the appropriate case and discard for when app is in foreground.

    In iOS 10, to handle notification when app is terminated and you launch the app from the notification, handle it in

    func application(_ application: UIApplication,  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
     if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {
         if  let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary
    
        } 
     }
    
    }
    

    For handling notification for foreground, use this method

     func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
         //handle notification here
    
     }
    

    For handling notification for background, use this method:

     func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
         //handle notification here
    
     }
    

提交回复
热议问题