在前台时不显示通知

Jeg*_*ggy 3 push-notification ios firebase swift firebase-admin

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

它是我的有效载荷的东西还是我必须在AppDelegate.swift中做的事情?

Kar*_*raj 8

你可以AppDelegate通过使用applicationState,

在iOS8中:

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

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

}
Run Code Online (Sandbox Code Playgroud)

在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])
    }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

谢谢.