本地通知"didReceiveLocalNotification"调用两次

Nir*_*raj 13 iphone notifications ios uilocalnotification

我使用以下方式处理本地通知:

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
Run Code Online (Sandbox Code Playgroud)

并安排本地通知:

- (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");
}
Run Code Online (Sandbox Code Playgroud)

当我收到通知时,didReceiveLocalNotification:会被叫两次.

难道我做错了什么?

请帮忙.

谢谢.

jmd*_*mbe 27

我认为模拟器中存在一个已知错误,它会两次触发委托通知方法.它不应该发生在设备上,不管是否与XCode相连.

  • 我不确定为什么,但我也在设备上面对这个问题(didReceiveLocalNotification多次),所以我维护了一个名为status的字段,如果已经被触发,则手动检查该通知的状态字段 (7认同)

Muh*_*qib 14

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

if (state == UIApplicationStateActive) {
    NSLog(@"UIApplicationStateActive"); 
}
else if(state == UIApplicationStateInactive){
    NSLog(@"UIApplicationStateInActive");
}
Run Code Online (Sandbox Code Playgroud)

在这些情况下,我只是编写代码,我希望我的应用程序在通知,活动模式和非活动模式下执行