使用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 }

 

转载于:https://www.cnblogs.com/jackma86/p/4982169.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值