在前台时不显示通知
Don't show notification when in foreground
我正在使用 Firebase admin SDK 从我的节点服务器发送推送通知。但是在 iOS 上,我只想在应用程序位于 background/terminated 中而不是在前台时显示通知。目前它将始终显示通知。
这是我的负载:
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 中做的事情?
您可以在 AppDelegate
中使用 applicationState
、
在iOS8中:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if !UIApplication.shared.applicationState == .active {
// Handle your push notification here
}
}
在iOS10中:
import UserNotifications
框架
实施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
}
我正在使用 Firebase admin SDK 从我的节点服务器发送推送通知。但是在 iOS 上,我只想在应用程序位于 background/terminated 中而不是在前台时显示通知。目前它将始终显示通知。
这是我的负载:
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 中做的事情?
您可以在 AppDelegate
中使用 applicationState
、
在iOS8中:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if !UIApplication.shared.applicationState == .active {
// Handle your push notification here
}
}
在iOS10中:
import UserNotifications
框架实施
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
}