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

使用过的代码,直接贴上

 UILocalNotification *notification = [[UILocalNotification alloc] init];

   if (notification!=nil) {

     NSDate *now = [NSDate new];

     //从现在开始,10秒以后通知

     notification.fireDate=[now addTimeInterval:10];

     //使用本地时区

     notification.timeZone=[NSTimeZone defaultTimeZone];

     notification.alertBody=@"顶部提示内容,通知时间到啦";

     //通知提示音 使用默认的

     notification.soundName= UILocalNotificationDefaultSoundName;

     notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil);

     //这个通知到时间时,你的应用程序右上角显示的数字。

     notification.applicationIconBadgeNumber = 1;

     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]];

 }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知...
    莫离_焱阅读 11,594评论 1 8
  • 许多集成的步骤个推官网都有了,这里只写关于推送的远程推送和本地通知的步骤和代码。APP在后台时:走苹果的APNS通...
    AllureJM阅读 7,768评论 1 9
  • //发送通知 UILocalNotification *notification=[[UILocalNotific...
    韩七夏阅读 5,536评论 1 0
  • 极光推送: 1.JPush当前版本是1.8.2,其SDK的开发除了正常的功能完善和扩展外也紧随苹果官方的步伐,SD...
    Isspace阅读 11,715评论 10 16
  • 电影《寿司之神》是由大卫·贾柏拍摄的关于全球最年长的三星大厨小野二郎的纪录片,今年88岁的小野二郎是全球最年长的三...
    京城浮世荟阅读 4,231评论 0 1