简体   繁体   English

推送通知徽章自动增加

[英]Push-Notification Badge auto increment

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.也许有一种方法可以在 JSON 徽章字段中发送一些诸如“++”之类的变量。 Any hack for that?任何黑客? Or do I need to go with the counting system server-sided??还是我需要使用服务器端的计数系统?

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. 2014 年 3 月更新:有关可能的更新,请参阅评论。 I haven't done pushes in several years so haven't been able to verify this myself.我已经好几年没有做过推送了,所以我自己也无法验证这一点。

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).您始终可以将未读总数作为附加 JSON 值作为推送有效负载的一部分发送(推送会忽略它没有明确理解的键)。 Once the user opens the app, read the value and adjust the badge programmatically yourself via UIApplication's applicationIconBadgeNumber property.用户打开应用程序后,读取值并通过 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 (ie 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.在这些情况下,您的徽章不会改变,但是一旦他们运行应用程序(如果他们点击“查看”),那么您的应用程序就可以将其设置正确。

It is now possible to have the client auto-increment the badge using a UNNotificationServiceExtension.现在可以让客户端使用 UNNotificationServiceExtension 自动增加徽章。

Extensions have the ability to modify notification payloads before iOS displays them.扩展能够在 iOS 显示之前修改通知负载。 In summary, store a badge counter in UserDefaults and modify the notification's badge count as needed.总之,在 UserDefaults 中存储一个徽章计数器并根据需要修改通知的徽章计数。 You'll need to add the App Groups capability to share User Defaults.您需要添加应用程序组功能以共享用户默认值。

Here's a detailed step-by-step guide: https://prodocs.cometchat.com/docs/ios-increment-app-icon-badge-count这是详细的分步指南: https : //prodocs.cometchat.com/docs/ios-increment-app-icon-badge-count

You can try out App42 backend services which provide auto increment of push badge count which is maintained on the server side.您可以尝试 App42 后端服务,这些服务提供在服务器端维护的推送徽章计数的自动递增。 For more details you can follow the link of blog .有关更多详细信息,您可以关注博客的链接。 Here is the blogpost conent:以下是博文内容:

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())

NB: 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. updatePushBadgeforUser – 此方法用于更新用户采购的所有设备的推送徽章计数。 In this case, we are assuming that the user has multiple devices registered under his name.在这种情况下,我们假设用户在其名下注册了多个设备。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM