localNotification 运行不正常。 didReceiveLocalNotification 函数没有在合适的时候调用
The localNotification is not working well. The function didReceiveLocalNotification is not called at the good moment
我创建了一个 localNotification 来发送一条消息,但它并没有在适当的时候被调用。奇怪的是,当我把日期放在未来时,它永远不会触发它,但如果我把日期放在过去,它会在应用程序启动时触发它...
主要功能:
...
NSDateComponents *comps = [NSDateComponents new];
[comps setDay:28];
[comps setMonth:02];
[comps setYear:2017];
[comps setHour:16];
[comps setMinute:50];
[comps setSecond:00];
NSDate *alarmDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSDate *currentDate= [NSDate date];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:alarmDate];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber=1;
// Setup alert notification
[localNotification setAlertAction:@"Open App"];
// [localNotification setAlertBody:[randonQuotesDic objectForKey:@"Rajneesh"]];
[localNotification setAlertBody:@"You had set a Local Notification on this time"];
localNotification.soundName=UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
...
函数接收本地通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
// check state here
if(state ==UIApplicationStateBackground ){
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Wake" message:notification.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
});
}
无法正常工作...
答案如下:
您只需授权即可。您必须在主函数中初始化 localNotification 之前放置这些行:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
我创建了一个 localNotification 来发送一条消息,但它并没有在适当的时候被调用。奇怪的是,当我把日期放在未来时,它永远不会触发它,但如果我把日期放在过去,它会在应用程序启动时触发它...
主要功能:
...
NSDateComponents *comps = [NSDateComponents new];
[comps setDay:28];
[comps setMonth:02];
[comps setYear:2017];
[comps setHour:16];
[comps setMinute:50];
[comps setSecond:00];
NSDate *alarmDate = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSDate *currentDate= [NSDate date];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:alarmDate];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber=1;
// Setup alert notification
[localNotification setAlertAction:@"Open App"];
// [localNotification setAlertBody:[randonQuotesDic objectForKey:@"Rajneesh"]];
[localNotification setAlertBody:@"You had set a Local Notification on this time"];
localNotification.soundName=UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
...
函数接收本地通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
// check state here
if(state ==UIApplicationStateBackground ){
}
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Wake" message:notification.alertBody delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alertView show];
});
}
无法正常工作...
答案如下:
您只需授权即可。您必须在主函数中初始化 localNotification 之前放置这些行:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];