使用UILocalNotification给App添加本地消息通知

时间:2023-03-08 22:35:22
使用UILocalNotification给App添加本地消息通知

使用过的代码,直接贴上

 UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification!=nil) {
NSDate *now = [NSDate new];
//从现在开始,10秒以后通知
notification.fireDate=[now addTimeInterval:];
//使用本地时区
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.alertBody=@"顶部提示内容,通知时间到啦";
//通知提示音 使用默认的
notification.soundName= UILocalNotificationDefaultSoundName;
notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil);
//这个通知到时间时,你的应用程序右上角显示的数字。
notification.applicationIconBadgeNumber = ;
NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
notification.userInfo = dic;
//启动这个通知
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
}

需要注意的是在iOS8之后需要注册消息推送服务才可以,具体实现就在AppDelegate的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法中直接调用下面方法即可

 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound
categories:nil]];
}