我的应用程序有一个使用 UILocalNotification 的闹钟功能,并且效果很好。但是,如果用户卸载该应用程序,然后重新安装它,他将立即收到所有“中间”通知。
我尝试调用:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
如果这是应用程序第一次启动,但它没有帮助,因为甚至在调用 application:didFinishLaunchingWithOptions: 之前就收到了通知。
在 4.0 中情况更糟,即使用户删除了应用程序,警报也会重复出现,但至少 Apple 在以后的版本中修复了该错误。然而现在我被这个问题困住了。有人有想法吗?
最佳答案
根据苹果公司的说法,这不是一个错误(我提交了一个错误报告)。系统会将已卸载应用程序的 UILocalNotifications 保留 24 小时,以防用户意外删除该应用程序,并在该时间范围内重新安装该应用程序时恢复所述 UILocalNotifications。
解决方案是在首次启动时删除所有 UILocalNotification,如下所示:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
当然,实现您自己的 firstRun
setter 和 getter 来获取/保存到持久存储中,例如 NSUserDefaults
。
关于iphone - 重新安装应用程序后 UILocalNotification 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4923028/