objective-c 推送通知徽章自动增加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1942605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:38:41  来源:igfitidea点击:

Push-Notification Badge auto increment

iphoneobjective-ciphone-sdk-3.0push-notification

提问by natanavra

I've been implementing the push service to my application, and I've been thinking about the application's badge. My app is a mail app (sorta) and I want to notify the user via push for new messages added to the inbox, I want the badge = number of new messages in the inbox.

我一直在为我的应用程序实施推送服务,我一直在考虑应用程序的徽章。我的应用程序是一个邮件应用程序(sorta),我想通过推送通知用户将新消息添加到收件箱中,我希望徽章 = 收件箱中的新消息数。

I thought of doing it server sided (provider) checking for new messages and sending the number as the badge.

我想在服务器端(提供商)检查新消息并将号码作为徽章发送。

The question is: Is there a way to auto-increment the application's badge, without having to calculate the badge value server sided and afterwards sending it as a part of the push payload to the APSN?

问题是:有没有办法自动增加应用程序的徽章,而不必计算服务器端的徽章值,然后将其作为推送有效载荷的一部分发送到 APSN?

Maybe there's a way to send in JSON badge field some variable like "++" or something like that. Any hack for that? Or do I need to go with the counting system server-sided??

也许有一种方法可以在 JSON 徽章字段中发送一些诸如“++”之类的变量。任何黑客?还是我需要使用服务器端的计数系统?

回答by bpapa

Nope, you'll have to track this on the server side. If you don't include any value for badge, it will get removed completely.

不,你必须在服务器端跟踪这个。如果您不包含任何徽章值,它将被完全删除。

Of course though this is only if the user receives the notification and the app isn't running/they choose not to launch it. If the user launches the app or already had it running you can do whatever you want in regards to incrementing.

当然,这只是在用户收到通知并且应用程序未运行/他们选择不启动它的情况下。如果用户启动了应用程序或已经运行了它,你可以做任何你想做的关于递增的事情。

UPDATE March 2014:See comments for a possible update. I haven't done pushes in several years so haven't been able to verify this myself.

2014 年 3 月更新:有关可能的更新,请参阅评论。我已经好几年没有做过推送了,所以我自己也无法验证这一点。

回答by Ramin

It's sort of possible but there's a trade-off.

这是可能的,但需要权衡。

You can always send up the unread total as an add-on JSON value as part of the push payload (push ignores keys it doesn't explicitly understand). Once the user opens the app, read the value and adjust the badge programmatically yourself via UIApplication's applicationIconBadgeNumberproperty.

您始终可以将未读总数作为附加 JSON 值作为推送有效负载的一部分发送(推送会忽略它没有明确理解的键)。用户打开应用程序后,读取值并通过 UIApplication 的applicationIconBadgeNumber属性自己以编程方式调整徽章。

The problem with doing it that way is that push adjusts the badge value even if the user doesn't open the app (i.e. when they get the notice and the user hits 'Cancel' instead of 'View'). In those cases your badge won't change, but as soon as they run the app (if they hit 'View') then your app can set it right.

这样做的问题是,即使用户没有打开应用程序(即,当他们收到通知并且用户点击“取消”而不是“查看”时),推送也会调整徽章值。在这些情况下,您的徽章不会改变,但是一旦他们运行应用程序(如果他们点击“查看”),那么您的应用程序就可以将其设置正确。

回答by user3663334

You can try out App42 backend services which provide auto increment of push badge count which is maintained on the server side. For more details you can follow the link of blog. Here is the blogpost conent:

您可以尝试 App42 后端服务,这些服务提供自动增加推送徽章计数,该计数在服务器端维护。有关更多详细信息,您可以关注博客的链接。以下是博文内容:

Here are the few use cases that can be achieved through auto incremental badge count in App42 Push Notification.

以下是可以通过 App42 推送通知中的自动递增徽章计数来实现的少数用例。

For auto increment of push badge by 1, you need to send push message as shown below.

要使推送徽章自动增加 1,您需要发送推送消息,如下所示。

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string message= "{'badge':'increment'}";
pushNotificationService.SendPushMessageToUser(userName,message, new UnityCallBack())

N.B: The sample explained is for Unity/C# but the same process can be applied on others too.

注意:所解释的示例适用于 Unity/C#,但同样的过程也适用于其他人。

If you want to stipulate any number for the badge or want to reduce the badge count to zero, you can use this method to update the count as the notification gets clicked by the user. You have to call updatePushBadgeforDevice or updatePushBadgeforUser in this case.

如果您想为徽章规定任何数量或希望将徽章计数减少到零,您可以使用此方法在用户单击通知时更新计数。在这种情况下,您必须调用 updatePushBadgeforDevice 或 updatePushBadgeforUser。

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string deviceToken = "DeviceToken";
int badges = 10; // For clear count make it 0 
pushNotificationService.UpdatePushBadgeforDevice(userName, deviceToken, badges,  new UnityCallBack());

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
int badges = 10; // For clear count make it 0
pushNotificationService.UpdatePushBadgeforUser(userName, badges,  new UnityCallBack());

updatePushBadgeforDevice– This method is used to update push badge count of a particular device registered by the user .

updatePushBadgeforDevice– 此方法用于更新用户注册的特定设备的推送徽章计数。

updatePushBadgeforUser– This method is used to update push badge count of all the devices that a user procures. In this case, we are assuming that the user has multiple devices registered under his name.

updatePushBadgeforUser– 此方法用于更新用户采购的所有设备的推送徽章计数。在这种情况下,我们假设用户在其名下注册了多个设备。

回答by user3749812

Send +1for badge count, which will auto-increment the badge count by 1.

发送+1徽章计数,这将自动将徽章计数增加 1。