我正在運行後臺代碼來檢查網站是否有任何更改,以及是否有更改本地通知以提醒用戶。但是,當我運行代碼並嘗試出局部通知沒有到達。 這是我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]];
}
}
代碼我做日誌「發送通知」,但我沒有收到任何通知。有什麼缺失或錯誤?我的其他無效操作中沒有任何其他代碼。
編輯 在@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.
當我模擬後臺提取時,此警告是否正常?我的通知仍然不會顯示。我將如何解決它?
您不能在後臺使用用戶計時器。當應用程序進入後臺並使用「fireDate」將通知延遲60秒時,您可以設置「UILocalNotification」。 – rckoenes
但是,如果我不使用計時器,它只會檢查更改只有一次 – Chandran
是的只是從'applicationDidEnterBackground:'調用'backGroundParsing',它應該工作。 – rckoenes