如何在后台模式中计数(跟踪)本地通知?

时间:2013-11-27 09:02:43

标签: ios iphone notifications uilocalnotification

我有一个本地通知,每分钟都会触发一次。我做了那个部分。但我的问题是我想计算通知和(计数= 15)然后它就停止了。我可以在应用程序运行状态中执行该部分。如何在后台模式下实现?

而且我还想跟踪有多少通知被点燃?是否可以在后台模式中使用。

//below is my notification delegate method
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
   if (notification) { 
   // notification code goes here
   }
}

任何演示代码都会欣赏。提前谢谢。

2 个答案:

答案 0 :(得分:2)

你真的不能这样做。唯一的方法是连续设置15个通知,并保留自己的日期列表。 iOS不允许您访问通知列表或任何内容。如果用户阻止他们,他们不必被解雇。

所以我建议用事件+日期制作自己的列表,并独立于通知管理它们 - 特别是当你有一些重要的逻辑要运行时,你不能使用通知。

答案 1 :(得分:0)

将您的计数器保存在didReceiveLocalNotification的用户默认值中:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int counter = [[userDefaults valueForKey:@"notiCounter"] intValue];
counter =+ 1;
if (counter == 15) {

} else {
    [userDefaults setValue:[NSString stringWithFormat:@"%i",counter] forKey:@"notiCounter"];
    [userDefaults synchronize];
}

当然你必须初始化计数器并存储在didFinishLaunchingWithOptions中。