2017-05-05 70 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 

}