【发布时间】:2018-06-30 17:10:37
【问题描述】:
使用的技术:
- Firebase Cloud Messaging
- 斯威夫特 3.2
- Xcode 9.2
为了给你一个背景,这里是我通过 FCM 在 api 服务器上发送的数据
{
"priority": "high",
"content_available": true,
"alert": true,
"sound": true,
"registration_ids": [
"<SOME_ID_HERE>",
"<SOME_ID_HERE>"
],
"notification": {
"title": "SOME TITLE",
"body": "SOME BODY"
},
"data": {
"some_key": "some_value",
"some_key2": "some_value2"
}
}
应用程序正确接收了此信息,并且我以正确的方式正确使用了这些方法
application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage)
messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)
userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
当应用程序处于后台/使用此示例代码终止时,有没有办法以某种方式忽略推送通知?
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
switch application.applicationState {
case .inactive, .background:
// Tell Firebase that we recieved the Push Notification
self.messaging.appDidReceiveMessage(userInfo)
// `PushNotification` is a model that parses the `userInfo` dictionary
guard let pushNotification: PushNotification = PushNotification(userInfo) else {
return // end code execution cause push cannot be parsed
}
if pushNotification.isSomeKindOfThing {
// ignore
} else {
// do nothing so it will show
}
case .active:
// handle when application is active
}
}
我的目标是,当我收到推送通知时,我可以忽略它,具体取决于 "data" 键中的内容。
【问题讨论】:
-
您不能,当应用程序处于非活动状态或终止时,操作系统会处理接收通知并将通知传递给应用程序。对您想要忽略的通知和您希望 iOS 与用户亲密的普通通知使用静默通知
-
@SandeepBhandari 是的,但问题在于我们的后端推送发送工作应该如何进行重组。
-
对不起,我不确定你的意思是什么,如果你的问题是我的服务器将如何知道应用程序是在后台还是前台来相应地构造有效负载,Quickblox 聊天使用这种方法框架,他们有一个 API,每次客户端应用程序进入后台时都会点击该 API,applicationDidEnterBackground 使用后台会话来确保 API 调用的成功完成。这意味着您需要在您的服务器上提供有关每个设备状态的不必要信息。如果你问值不值,我会说不。但是,如果那是您的要求,请继续修改您的后端代码
-
@SandeepBhandari 抱歉,这是进一步的解释,我所说的后端重组的意思是,如果我要使用静默通知,则需要更改当前用于发送通知的后端 api 实现,是的,我知道怎么做,但我目前正在寻找一种方法,如果可能的话,可以通过移动端处理这个问题。
-
@zonily-jame :嗯,有趣,抱歉,但目前没有建议:P 据我所知这是不可能的,但可能有办法做到这一点,所以我也会期待对于此处发布的答案:)
标签: ios push-notification firebase-cloud-messaging swift3.2