簡體   English   中英

在前台時不顯示通知

[英]Don't show notification when in foreground

我正在使用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中做的事情?

您可以使用applicationStateAppDelegate實現此目的,

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

謝謝。

在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

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM