【问题标题】:Don't show notification when in foreground在前台时不显示通知
【发布时间】:2017-10-03 18:13:14
【问题描述】:

我正在使用 Firebase 管理 SDK 从我的节点服务器发送推送通知。但在 iOS 上,我只想在应用程序处于后台/终止时显示通知,而不是在前台显示通知。目前它会一直显示通知。

这是我的有效载荷:

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);

是我的有效载荷中的某事还是我必须在 AppDelegate.swift 中执行的某事?

【问题讨论】:

    标签: ios swift firebase push-notification firebase-admin


    【解决方案1】:

    您可以在AppDelegate 中使用applicationState 实现此目的,

    在 iOS8 中:

      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    
        if !UIApplication.shared.applicationState == .active {
         // Handle your push notification here   
        }
    
    }
    

    在 iOS10 中:

    1. import UserNotifications 框架
    2. 实现UNUserNotificationCenterDelegate 方法

      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])
      }
      
      }
      

    谢谢。

    【讨论】:

      【解决方案2】:

      在 iOS 中,您在 AppDelegate.swift 中决定如何处理通知。在 AppDelegate 中处理适当情况的通知,并在应用处于前台时丢弃。

      在 iOS 10 中,要在应用终止并从通知启动应用时处理通知,请在

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

      前台处理通知,使用此方法

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

      对于后台处理通知,使用这个方法:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-14
        • 2019-08-05
        • 2022-01-06
        • 2019-06-24
        • 1970-01-01
        • 2017-11-23
        相关资源
        最近更新 更多