本地通知“didReceiveLocalNotification”打电话两次

发布于 2024-09-09 05:13:09 字数 1278 浏览 3 评论 0原文

我正在使用以下方法处理本地通知:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过期情话 2024-09-16 05:13:09

我认为模拟器中有一个已知的错误,它会触发委托通知方法两次。它不应该发生在设备上,无论是否与 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.

空‖城人不在 2024-09-16 05:13:09

我也面临同样的问题,我找到的解决方案是在 didReceiveLocalNotification 中编写此代码,

if (state == UIApplicationStateActive) {
    NSLog(@"UIApplicationStateActive"); 
}
else if(state == UIApplicationStateInactive){
    NSLog(@"UIApplicationStateInActive");
}

在这些情况下,我只需编写我希望我的应用程序在活动模式下在通知上执行的代码,并且处于非活动模式

i was also facing the same problem and the solution which i find is that write this code in didReceiveLocalNotification

if (state == UIApplicationStateActive) {
    NSLog(@"UIApplicationStateActive"); 
}
else if(state == UIApplicationStateInactive){
    NSLog(@"UIApplicationStateInActive");
}

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

心意如水 2024-09-16 05:13:09

我怀疑只要在同一秒内通知就会被重新触发。我通过在处理程序中将 fireDate 设置为 nil 来修复它:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

    notification.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:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:notification.alertAction message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];

    notification.fireDate = nil;

}
娇纵 2024-09-16 05:13:09

我也有同样的问题。这是由于在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文