[英]UILocalNotification fires after reinstalling the app
My app has an alarm function using UILocalNotification, and it works great.我的应用程序有一个使用 UILocalNotification 的闹钟功能,而且效果很好。 However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.
但是,如果用户卸载该应用程序,然后重新安装它,他将立即收到所有“中间”通知。
I have tried to call:我试过打电话:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.如果这是第一次启动应用程序,但它没有帮助,因为甚至在 application:didFinishLaunchingWithOptions: 被调用之前就收到了通知。
This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release.这在 4.0 中更糟,即使用户删除了应用程序也会重复警报,但至少苹果在以后的版本中修复了这个错误。 However now I'm stuck with this.
但是现在我坚持这个。 Anyone has an idea?
有人有想法吗?
According to Apple, this is not a bug (I filed a bug report).根据 Apple 的说法,这不是错误(我提交了错误报告)。 The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.
系统会将已卸载应用程序的 UILocalNotifications 保留 24 小时,以防用户意外删除该应用程序,并在该时间范围内重新安装该应用程序时恢复所述 UILocalNotifications。
The solution would be to remove all UILocalNotifications on first startup, like so:解决方案是在第一次启动时删除所有 UILocalNotifications,如下所示:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
of course, implement your own firstRun
setter and getter to fetch/save into persistent storage, like NSUserDefaults
.当然,实现你自己的
firstRun
setter 和 getter 来获取/保存到持久存储中,比如NSUserDefaults
。
This is actually a bug in iPhone.这实际上是 iPhone 中的一个错误。 If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.
如果您删除该应用程序并稍后安装它,它将具有相同的应用程序 ID,因此当重新安装该应用程序时,即使您没有打开该应用程序,也会触发所有过去的本地通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.