本地通知未到达

时间:2015-10-21 09:09:21

标签: ios uilocalnotification

我正在运行后台代码,以检查网站是否有任何更改,以及是否有更改本地通知以提醒用户。但是,当我运行代码并尝试它时,本地通知不会到达。 这是我的appDelegate.m

中的代码
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Has Entered Background");
    timerCountDown = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(backGroundParsing) userInfo:nil repeats:YES];
}

- (void)backGroundParsing {
    //This is where my background checking happens. I deleted the code here to make it shorter.
    if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
                                NSLog(@"Notificatoin sent");
                                NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
                                UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
                                [notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
                                [notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
                                [notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
                                [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
                            }
}

我记录“发送通知”但我没有收到任何通知。有什么东西丢失或有什么不对吗?我的其他无效行动中没有任何其他代码。

EDIT 之后@rckoenes说我编辑了我的代码。

- (void)application:(UIApplication * _Nonnull)application
performFetchWithCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult result))completionHandler {
    [self backGroundParsing];
}

- (void)backGroundParsing {
    //This is where my background checking happens. I deleted the code here to make it shorter.
                            if (totalNumberOfEvents > totalNumberOfEvetsRecorded) {
                                NSLog(@"Notificatoin sent");
                                NSString *finalNumberOfEventsRecorded = [NSString stringWithFormat:@"%d",totalNumberOfEvents];
                                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                                [userDefaults setObject:finalNumberOfEventsRecorded forKey:@"finalRegisteredNumberOfEvents"];
                                UILocalNotification *notifyUserAboutNewEvent = [[UILocalNotification alloc] init];
                                [notifyUserAboutNewEvent setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
                                [notifyUserAboutNewEvent setTimeZone:[NSTimeZone defaultTimeZone]];
                                [notifyUserAboutNewEvent setAlertBody:@"New event has been organised"];
                                [[UIApplication sharedApplication] setScheduledLocalNotifications:[NSArray arrayWithObject:notifyUserAboutNewEvent]];
                            }
                        });
     }];
    [searchEventsTask resume];
}

但是,我仍然没有收到通知。当我模拟背景提取时,我收到警告。这是我的NSLog Warning: Application delegate received call to -application:performFetchWithCompletionHandler: but the completion handler was never called.中出现的警告 当我模拟背景提取时,此警告是否正常?我的通知仍然没有出现。我该如何解决?

1 个答案:

答案 0 :(得分:1)

在iOS 8+中,您必须获得用户对本地通知的许可。在application didFinishLaunchingWithOptions方法中添加以下行:

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];