我创建了一个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];
});
}
它无法正常工作......
答案 0 :(得分:0)
以下是答案:
你必须简单地授权它。您必须在主函数初始化localNotification之前放置那些行:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];