在前台不显示通知 [英] Don't show notification when in foreground
问题描述
我使用Firebase管理软件开发工具包从我的节点服务器发送推送通知。但在iOS上,我只想显示通知时,应用程序是在后台/终止,而不是在前台。
const payload = {
data:{
data:'data',
more:'moreData',
},
通知:{
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中做的事情? 使用<$ c>可以在 AppDelegate
在iOS8中:
$ b
func application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any]){
if!UIApplication.shared.applicationState == .active {
//在此处理您的推送通知
$ b code $
在iOS10中:
-
import UserNotifications
框架
-
实现 UNUserNotificationCenterDelegate
方法
func userNotificationCenter(_ center:UNUserNotificationCenter,willPresent notification:UNNotification,withCompletionHandler completionHandler :@escaping(UNNotificationPresentationOptions) - >无效){
如果UIApplication.shared.applicationState == .active {//在iOS 10中,如果应用程序处于前景,则什么也不做。
completionHandler([])
} else {//如果应用程序不活跃,您可以显示横幅,声音和徽章。
completionHandler([。alert,.badge,.sound])
}
}
谢谢。
I'm sending push notification from my node server using the Firebase admin SDK. But on iOS I only want to show the notification when the app is in the background/terminated and not when in the foreground. Currently it will always show the notification.
This is my payload:
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);
Is it something with my payload or is it something that I have to do in the AppDelegate.swift?
解决方案 You can achieve this in AppDelegate
by using the applicationState
,
In iOS8:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if !UIApplication.shared.applicationState == .active {
// Handle your push notification here
}
}
In iOS10:
import UserNotifications
framework
Implement UNUserNotificationCenterDelegate
method
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])
}
}
Thanks.
这篇关于在前台不显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!