【发布时间】:2015-02-06 13:41:18
【问题描述】:
我正在我的应用程序中处理警报模块。我开发了代码,该代码从用户那里获取日期和时间输入,并在我的应用程序中的特定时间设置事件。 我还将信息存储到我的本地数据库中,以显示用户设置的警报列表。
现在我想在执行特定警报时从数据库中删除条目(当 UILocalNotification 显示到应用程序中时,我想调用数据库方法从数据库中删除该条目)
我是这样设置通知的
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
int notificationCount = [preferences integerForKey:@"notification_count"];
notificationCount++;
NSDate* final_date = [calendar dateFromComponents:final_Components];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = final_date;
localNotification.alertBody=titleTextField.text;localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSDictionary* userInfoDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:notificationCount] forKey:@"notification_id"]; localNotification.userInfo = userInfoDic;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[preferences setInteger:notificationCount forKey:@"notification_count"];
我使用了委托 didReceiveLocalNotification
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:@"notification_id"];
NSLog(@"userInfo is %@",itemName);
databaseObject.deleteNotification(itemName)
}
我的问题是“didReceiveLocalNotification”仅在 2 种情况下调用
1) 用户使用应用时(前台应用)。
2) 当通知显示并且用户点击通知时
但是在第三种情况下,当应用程序处于后台模式并显示通知并且用户不单击通知或第二种情况是打开应用程序时直接单击应用程序图标或用户清除通知时,不会调用 didReceiveLocalNotification 委托。 .
有什么方法可以在所有情况下检测通知触发,或者通过使用任何其他方法来检测通知已触发,然后我将执行我的删除方法。
感谢任何帮助 谢谢你
【问题讨论】:
-
抱歉,
UILocalNotification不是这样工作的。当应用程序在后台时,您不能使用它来执行代码,您必须找到另一种方法来实现这一点。后台执行在 iOS 上是一个棘手的问题,不幸的是它的使用仅限于某些场景 -
@nburk 是否可以在 applicationWillEnterForeground 委托中检查过去执行的 UILocalnotification?
-
我遇到了一些我想做的计划。最好的解决方案似乎是做一个数据推送通知,它允许进行一些处理,但是很难找到一个以合理的价格提供预定推送的服务,并且您可以从应用程序中进行配置。
标签: ios objective-c iphone notifications