我想知道是否可以通过Firebase向用户发送多个推送通知,但在用户在设备上看到它们之前拦截它们,并允许我的应用程序选择性地允许通知。有没有办法阻止用户从Firebase发送推送通知后收到推送通知?
这可能吗?
如果是这样,我需要使用哪种方法? (无论是从react-native-firebase
或iOS
)
我想知道是否可以通过Firebase向用户发送多个推送通知,但在用户在设备上看到它们之前拦截它们,并允许我的应用程序选择性地允许通知。有没有办法阻止用户从Firebase发送推送通知后收到推送通知?
这可能吗?
如果是这样,我需要使用哪种方法? (无论是从react-native-firebase
或iOS
)
新
您可以发送无声通知用户,并安排您的实际通知,本地通知
参考This
老
(在案例Firebase) 只有当应用处于前台时,通知才可管理。 如果应用程序处于后台,则无法处理该应用程序,因为用户将收到通知,并且只有在点按通知后才会通知应用程序。
我不会推荐这么做 - 许多事情可能会阻止无声通知的传递。例如,低电量模式,弱电池信号等等。该文档解释说,这些通知仅在iOS决定这样做时才处理,例如插入时,或者使用强大的无线或蜂窝信号时。 –
您可以尝试使用UNNotificationServiceExtension
(可从iOS 10
获得)。你需要为你的项目添加一个扩展名(example how to add)。
执行方法- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler;
在你的扩展中。
它会在通知显示给用户之前调用,并且您可以尝试执行一些操作(例如:将一些数据保存到本地)。
注意它只适用于iOS 10 rich notifications
(how to implement it with Firebase)。
P.S.希望它对你有所帮助!
EDIT:
覆盖此方法,并使用它来修改与该通知递送的 UNNotificationContent 对象。
UPDATE:
你可以尝试从服务器无声推送通知发送,检查,并建立本地通知展现给用户,如果需要它。
你知道这样可以忽略通知吗? –
@MichaelCampsall,我编辑我的答案,只是寻找它。 –
“UNNotificationContent:本地或远程通知的不可编辑内容。”来自Apple文档。 此外,在文档中:“只要您不删除它,您也可以修改警报文本。如果内容对象不包含任何警报文本,系统将忽略您的修改并提供原始通知内容。” –
当应用程序处于前台状态时,可以处理FireBase通知。首先,您将控制器和info.plist中的Always(通常)更改为Always(始终)。 一旦你设置了它,你可以在你的App委托或viewController中控制通知。
您可以发送将在前台和后台收到的数据通知。在iOS中,它们由代理方法application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
进行处理,处理后您可以决定是否为用户创建推送。
@Update
在iOS接收数据消息(未推)上iOS10
func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) {
handleNotification(data: remoteMessage.appData)
}
低于10
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
handleNotification(data: userInfo)
completionHandler(UIBackgroundFetchResult.newData)
}
然后我刚检查是否自定义字段有我需要的数据并使用NotificationCenter创建本地通知
fileprivate func handleNotification(data: [AnyHashable : Any]) {
guard let topic = data["topic"] as? String else { return }
if data["receiver"] as? String == UserManager.shared.current(Profile.self)?.uuid && topic == "coupons" {
displayNotification(data: data)
}
}
fileprivate func displayNotification(data: [AnyHashable : Any]) {
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.title = data["notification_title"] as! String
content.body = data["notification_body"] as! String
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 1, repeats: false)
let request = UNNotificationRequest.init(identifier: data["topic"] as! String, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
if currentNotification != nil {
UIApplication.shared.cancelLocalNotification(currentNotification!)
}
let notification = UILocalNotification()
notification.fireDate = Date()
notification.category = data["topic"] as? String
notification.alertBody = data["notification_body"] as? String
if #available(iOS 8.2, *) {
notification.alertTitle = data["notification_title"] as? String
}
currentNotification = notification
UIApplication.shared.scheduleLocalNotification(currentNotification!)
}
}
只要记住这一点与数据通知不推送通知,但在Firebase中,您可以发送这两种类型。
你能告诉我一些决定是否推送给用户的代码吗? –
我编辑了我的答案,只是使用内置的通知中心,显示效果不算什么,但如果需要某些解释,请随时问 – Thandulfan
发送无声的远程通知"content-available" : 1
并决定是否要显示给用户。
{
"aps" = {
"content-available" : 1,
"sound" : ""
};
// add custom key value for message
}
如果你想展示给用户,获取定制的消息键值对,并触发用户本地通知显示。它也会在后台运行。 UIBackgroundMode
需要启用远程通知。
另一种选择是在Firebase中创建新的“受众群体”,而不是在推送通知发送后选择性地阻止推送通知。然后,当你想将'this_push_notification'发送给一组特定的用户时,你只能将它发送给'receive_this_push_notification = true'的用户组。这样,您只能将通知发送给应该首先接收的用户(该过滤发生在Firebase一侧)。 可根据用户在应用中经历的特定自定义事件以及人口统计选项的数量创建受众群体。 –
不幸的是,我们需要的可能的观众组合数量在数万人之中 –