2017-05-05 46 views
3

我使用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做些什麼?

回答

6

您可以通過使用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]) 
    } 
    
    } 
    

感謝。

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 

}