本地通知“didReceiveLocalNotification”打电话两次
我正在使用以下方法处理本地通知:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
并安排本地通知:
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *fireDate = [NSDate date];
localNotif.fireDate = [fireDate dateByAddingTimeInterval:minutesBefore*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = kCFCalendarUnitMinute;
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"LocalEvent notification in %i minutes.", nil),minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"This is dict, you can pass info for your notification",@"info",nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
NSLog(@"Event scheduled");
}
当我收到通知时,didReceiveLocalNotification:
被调用两次。
我做错了什么吗?
请帮忙。
谢谢。
I am handling local notifications using:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
And to schedule a local notification:
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDate *fireDate = [NSDate date];
localNotif.fireDate = [fireDate dateByAddingTimeInterval:minutesBefore*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval = kCFCalendarUnitMinute;
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"LocalEvent notification in %i minutes.", nil),minutesBefore];
localNotif.alertAction = NSLocalizedString(@"View Details", nil);
localNotif.applicationIconBadgeNumber = 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"This is dict, you can pass info for your notification",@"info",nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
NSLog(@"Event scheduled");
}
When I receive a notification, didReceiveLocalNotification:
is called twice.
Am I doing something wrong?
Please help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为模拟器中有一个已知的错误,它会触发委托通知方法两次。它不应该发生在设备上,无论是否与 XCode 绑定。
I think there is a known bug in the simulator, that fires the delegate notification method twice. It should not happen on the device, tethered to XCode or not.
我也面临同样的问题,我找到的解决方案是在 didReceiveLocalNotification 中编写此代码,
在这些情况下,我只需编写我希望我的应用程序在活动模式下在通知上执行的代码,并且处于非活动模式
i was also facing the same problem and the solution which i find is that write this code in didReceiveLocalNotification
here in these condition i just write the code which i want my application to do on notification , in Active mode and in inactive mode
我怀疑只要在同一秒内通知就会被重新触发。我通过在处理程序中将 fireDate 设置为 nil 来修复它:
I suspect that the notification is being retriggered as long as its in the same second still. I fixed it by setting the fireDate to nil in the handler:
我也有同样的问题。这是由于在 AppDelegate 的“didFinishLaunchingWithOptions”中调用“registerUserNotificationSettings”两次引起的。然而,仅仅删除重复的调用并不能解决问题。我必须删除该应用程序然后重建。直到那时,双重本地通知问题才得到解决。
I had the same issued. It was caused by calling 'registerUserNotificationSettings' twice in the AppDelegate's 'didFinishLaunchingWithOptions.' However, simply removing the duplicate call did not fix the problem yet. I had to delete the app and then rebuild. Only then did the double local notification issue get fixed.