ios 在后台应用程序时使用推送通知更新徽章
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14256643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Update badge with push notification while app in background
提问by Abolfoooud
I have got push notification working and managed to update icon badge count when app is brought to foreground.
我有推送通知工作,并在应用程序被带到前台时设法更新图标徽章计数。
I am a bit confused about this though...the iPhone receives the notification and the pop up message appears to activate my app, and the badge only updates after me starting the app.
我对此有点困惑……iPhone 收到通知,弹出消息似乎激活了我的应用程序,并且徽章仅在我启动应用程序后更新。
This does not sound right in terms of user experience. My understanding is that the badge count should notify the user of what needs action, through incremented count, but this doe not happen until a later stage when the app is live.
就用户体验而言,这听起来并不正确。我的理解是,徽章计数应该通过递增计数通知用户需要采取什么行动,但这要到应用程序上线的后期阶段才会发生。
So is there a way to tell the app to update its badge count when it receives push notifications and whilst being in the background?
那么有没有办法告诉应用程序在收到推送通知时并在后台更新其徽章计数?
Note that my app does not use location and that I have UIRemoteNotificationTypeBadge
in the notification registration request.
请注意,我的应用程序不使用位置,并且我UIRemoteNotificationTypeBadge
在通知注册请求中使用了位置。
Cheers AF
干杯AF
采纳答案by rckoenes
Since push notification are handled by iOS and not your app you can't change the application badge on receiving a push notification.
由于推送通知由 iOS 而不是您的应用程序处理,因此您无法在收到推送通知时更改应用程序徽章。
But you can send the badge number in the payload of the push notification, but the you will have to do the calculation server side.
但是您可以在推送通知的有效负载中发送徽章编号,但是您必须在服务器端进行计算。
You should read Local and Push Notification Programming Guideand especially the The Notification Payload.
您应该阅读本地和推送通知编程指南,尤其是通知有效负载。
The payload could look like this:
有效载荷可能如下所示:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9
}
}
Now the app application badge icon will show 9.
现在应用程序徽章图标将显示 9。
回答by prajul
We can alter the badge number when we are in the background state by sending the "badge"parameter in the push notification package. As @rckoenes pointed out the JSON
parameter for the badge must be an INTEGER.
我们可以通过在推送通知包中发送“badge”参数来更改处于后台状态时的徽章编号。正如@rckoenes 指出的那样,JSON
徽章的参数必须是INTEGER。
Sample PHP code for doing the same
用于执行相同操作的示例 PHP 代码
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'badge' => 1,
'sound' => 'default'
);
badge => 1
where 1 is an integer not a string (i.e. without apostrophes)
badge => 1
其中 1 是整数而不是字符串(即没有撇号)
回答by Gkiokan
Actually in iOS 10 a remote Notification will call automaticly didReceiveRemoteNotification
Method in your AppDelegate.
实际上在 iOS 10 中,远程通知会自动调用didReceiveRemoteNotification
AppDelegate 中的方法。
You have 2 ways of updating the badge count in the background.
I've done this for my current app also. You don't need a Notification Service Exetension eithier.
您有 2 种在后台更新徽章计数的方法。
我也为我当前的应用程序做了这个。您不需要通知服务扩展。
1st Way
第一种方式
Send APS badge key with your payload to APN.
This will update the badge count according to your Integer value in your payload of badge. e.x.:
将带有负载的 APS 徽章密钥发送到 APN。
这将根据您的徽章有效载荷中的整数值更新徽章计数。前任:
// Payload for remote Notification to APN
{
"aps": {
"content-available": 1,
"alert": "Hallo, this is a Test.",
"badge": 2, // This is your Int which will appear as badge number,
"sound": default
}
}
2nd Way
第二种方式
You can switch your application.applicationState and update your badges Count when the applicationState is in .background
. BUT you have to take care not to set the badge key parameter in your Notification payload when sending to APN e.x.
您可以切换 application.applicationState 并在 applicationState 中更新您的徽章计数.background
。但是您必须注意不要在发送到 APN ex 时在通知负载中设置徽章键参数
// Payload to APN as silent push notification
{
"aps": {
"content-available": 1
}
}
Handle the badge Update accordingly to the application state
根据应用程序状态处理徽章更新
Here is my working code for badge count update without badge key in the payload for APN.
这是我的徽章计数更新的工作代码,APN 的有效负载中没有徽章密钥。
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("APN recieved")
// print(userInfo)
let state = application.applicationState
switch state {
case .inactive:
print("Inactive")
case .background:
print("Background")
// update badge count here
application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1
case .active:
print("Active")
}
}
Reset badge count
重置徽章计数
Don't forget to reset your badge count when your app gets back to active state.
当您的应用恢复到活动状态时,不要忘记重置您的徽章计数。
func applicationDidBecomeActive(_ application: UIApplication) {
// reset badge count
application.applicationIconBadgeNumber = 0
}
回答by Prabhat Pandey
**This is the APNS payload get back from server.**
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
The value for key badge is automatically considered as badge count.On ios app side no need to calculate or handle the count. In above example 9 is the badge count.So your app icon will show 9.
钥匙徽章的值被自动视为徽章计数。在 ios 应用程序端无需计算或处理计数。在上面的示例中,9 是徽章计数。因此您的应用程序图标将显示 9。
NOTE While your app is close u can't handle badges on your own.Thats why we are using badge key from APNS Payload For better clarification about notification see documentation
注意当您的应用程序关闭时,您无法自行处理徽章。这就是我们使用 APNS Payload 的徽章密钥的原因。有关通知的更好说明,请参阅文档
if you want to reduce the badge count on your own.Decrement the count and update it yourself.as follows
如果你想自己减少徽章数量。减少数量并自己更新。如下
回答by Surjeet Rajput
If you are using NotificationServiceExtension the you can update badge in that.
如果您使用的是 NotificationServiceExtension,则可以在其中更新徽章。
var bestAttemptContent : UNMutableNotificationContent? //
bestAttemptContent.badge = 0//any no you wanna display
Every time your application receive notification your service extension will be called.Save that value in user default and display it. To share user defaults between application and extension you need to enable app group in application. Read more here
每次您的应用程序收到通知时,您的服务扩展都会被调用。将该值保存在用户默认值中并显示它。要在应用程序和扩展程序之间共享用户默认值,您需要在应用程序中启用应用程序组。 在这里阅读更多
回答by Michael Krutoyarskiy
Since iOS 10 you can develop a Notification Service extension for your app. It will be started by the system when you receive a notification and you can calculate a valid number for the badge and set it.
从 iOS 10 开始,您可以为您的应用开发通知服务扩展。系统会在您收到通知时启动它,您可以计算徽章的有效编号并进行设置。
Take a look at the documentation: https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
看一下文档:https: //developer.apple.com/documentation/usernotifications/unnotificationserviceextension
回答by Александр Бабич
For Firebase Cloud Messaging (FCM) it should be like this:
对于 Firebase Cloud Messaging (FCM),它应该是这样的:
{
"to": "some_token",
"notification": {
"body": "this is a body",
"title": "this is a title",
"badge" : 1
},
"priority": "high",
}
回答by Javier J Solis Flores
in legacy its work, "badge" set count
在遗产中,“徽章”设置计数
{
"to": "token",
"notification": {
"title": "Example",
"body": "Tiene 22 actualizaciones.",
"badge":278
},
"data": {
"story_id": "story_12345",
"count_vaca":22
}
}
回答by mustafa
As @rckoenes said you will have to do the calculation server side, but how could you know when to increment the badge number value you should send in payload?.
正如@rckoenes 所说,您必须在服务器端进行计算,但是您怎么知道何时增加应在有效负载中发送的徽章编号值?
Will when you launch the application send a message to your server indicating that the application is have been launched. So, on the server side you start again from badge=0, and while there is no messages received by server increase the badge number with every push notification payload.
当您启动该应用程序时,将向您的服务器发送一条消息,表明该应用程序已启动。因此,在服务器端,您再次从徽章 = 0 开始,虽然服务器没有收到任何消息,但每个推送通知有效负载都会增加徽章编号。
回答by Prashant kumar
in apns payload have to define "content-available":1 for update badge count in background mode
在 apns 有效负载中必须定义“content-available”:1 以在后台模式下更新徽章计数
func application(_ application: UIApplication, didReceiveRemoteNotification
userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// increase badge count, but no need if you include content-available
application.applicationIconBadgeNumber = application.applicationIconBadgeNumber + 1
}
func applicationDidBecomeActive(_ application: UIApplication) {
// reset badge count
application.applicationIconBadgeNumber = 0
}
for eg.
例如。
"aps":{
"alert":"Test",
"sound":"default",
"content-available":1
}