使用UILocalNotification给App添加本地消息通知
使用过的代码,直接贴上
1 UILocalNotification *notification = [[UILocalNotification alloc] init]; 2 if (notification!=nil) { 3 NSDate *now = [NSDate new]; 4 //从现在开始,10秒以后通知 5 notification.fireDate=[now addTimeInterval:10]; 6 //使用本地时区 7 notification.timeZone=[NSTimeZone defaultTimeZone]; 8 notification.alertBody=@"顶部提示内容,通知时间到啦"; 9 //通知提示音 使用默认的 10 notification.soundName= UILocalNotificationDefaultSoundName; 11 notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil); 12 //这个通知到时间时,你的应用程序右上角显示的数字。 13 notification.applicationIconBadgeNumber = 1; 14 NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"]; 15 notification.userInfo = dic; 16 //启动这个通知 17 [[UIApplication sharedApplication]scheduleLocalNotification:notification]; 18 }
需要注意的是在iOS8之后需要注册消息推送服务才可以,具体实现就在AppDelegate的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中直接调用下面方法即可
1 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) { 2 [application registerUserNotificationSettings:[UIUserNotificationSettings 3 settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound 4 categories:nil]]; 5 }