简体   繁体   English

本地推送通知徽章编号

[英]local push notification badge number

I have a app where the user has various events they are able to check into. 我有一个应用程序,用户可以在其中查看各种事件。 If they check into the event, there will be a local notification setup for 24 hours before the event to remind the user. 如果检查到的情况下,会有一个本地通知设置24小时,在活动开始前提醒用户。 My question is, i have the setApplicationIconBadgeNumber for the app and i'm setting it to whatever the badge number is at the time, plus one. 我的问题是,我为该应用设置了setApplicationIconBadgeNumber ,并将其设置为当时的徽章编号,再加上一个。 Problem is say that there're two events that are at the same time, the badge number should be 2 not 1, how do I tell iOS to set the badge number appropriately? 问题是有两个事件同时发生,证件编号应为2而不是1,如何告诉iOS适当设置证件编号? Since this is all local notification and happens at a particular time, its hard to keep track within code... 由于这是所有本地通知,并且发生在特定时间,因此很难在代码内进行跟踪...

I agree it's hard to keep track of in your code, especially since with local notifications your code might not have a chance to actually execute when this is all happening. 我同意很难跟踪您的代码,尤其是在发生本地通知时,由于有了本地通知,您的代码可能没有机会真正执行。 Seems like an oversight in the API that could've been fixed with a special "increment badge number" event. 似乎是对API的疏忽,可以通过特殊的“递增徽章编号”事件来解决。

One solution is this: every time you create a new UILocalNotification , go through the list of all existing notifications and figure out what each of them should have as the correct badge number (eg, based on the fireDate of each). 一种解决方案是:每次创建新的UILocalNotification ,都要遍历所有现有通知的列表,并找出每个通知应具有的正确徽章编号(例如,基于每个的fireDate)。

So, use UIApplication 's scheduledLocalNotifications to get the array of existing applications. 因此,使用UIApplicationscheduledLocalNotifications获取现有应用程序的数组。 Sort them as necessary to figure out the correct badge number for each, and then update each object with the correct badge number. 根据需要对它们进行排序,以找出每个对象的正确徽章编号,然后使用正确的徽章编号更新每个对象。 Set the (modified) array back as the scheduledLocalNotifications property to register the updated values. 将(已修改的)数组重新设置为scheduledLocalNotifications属性,以注册更新的值。

Do the same thing whenever some unrelated external event changes the badge counter, and I think - though it's not pretty - you should have a reliable hack. 每当一些无关的外部事件更改徽章计数器时,请执行相同的操作,而且我认为-虽然不是很漂亮-但您应该拥有可靠的技巧。

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

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