本地通知未到达
Local notification not arriving
我正在运行宁后台代码检查网站是否有任何变化,如果有变化则本地通知提醒用户。但是,当我 运行 代码并尝试它时,本地通知不会到达。
这是我 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]];
}
}
我登录 "Notification sent" 但我没有收到任何通知。是不是少了什么东西或者出了什么问题?我的其他无效操作中没有任何其他代码。
编辑
在@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.
中的警告
当我模拟后台获取时,这个警告是否正常?我的通知仍然没有出现。我该如何解决?
在 iOS 8+ 中,您必须获得用户的本地通知许可。在您的 application didFinishLaunchingWithOptions
方法中添加以下行:
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
我正在运行宁后台代码检查网站是否有任何变化,如果有变化则本地通知提醒用户。但是,当我 运行 代码并尝试它时,本地通知不会到达。 这是我 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]];
}
}
我登录 "Notification sent" 但我没有收到任何通知。是不是少了什么东西或者出了什么问题?我的其他无效操作中没有任何其他代码。
编辑 在@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.
中的警告
当我模拟后台获取时,这个警告是否正常?我的通知仍然没有出现。我该如何解决?
在 iOS 8+ 中,您必须获得用户的本地通知许可。在您的 application didFinishLaunchingWithOptions
方法中添加以下行:
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];