Don't show notification when in foreground

老子叫甜甜 提交于 2019-12-30 11:01:30

问题


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 and not when in the foreground. Currently it will always show the notification.

This is my payload:

const payload = {
  data: {
    data: 'data',
    more: 'moreData',
  },
  notification: {
    title: 'Incoming Call',
    body: 'Someone is calling you',
    text: 'This is some text',
    sound: 'default',
    click_action: 'com.example.INCOMING_CALL',
  }
};
const options = {
  priority: 'high',
  time_to_live: 30,
  collapse_key: 'Video Call',
  content_available: true,
};

admin.messaging().sendToDevice(tokensList, payload, options);

Is it something with my payload or is it something that I have to do in the AppDelegate.swift?


回答1:


You can achieve this in AppDelegate by using the applicationState,

In iOS8:

  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {

    if !UIApplication.shared.applicationState == .active {
     // Handle your push notification here   
    }

}

In iOS10:

  1. import UserNotifications framework
  2. Implement UNUserNotificationCenterDelegate method

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    
    
    if UIApplication.shared.applicationState == .active { // In iOS 10 if app is in foreground do nothing.
        completionHandler([])
    } else { // If app is not active you can show banner, sound and badge.
        completionHandler([.alert, .badge, .sound])
    }
    
    }
    

Thanks.




回答2:


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

 }


来源:https://stackoverflow.com/questions/43801958/dont-show-notification-when-in-foreground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!