ios 远程通知(Remote Notification)和本地通知(Local Notification)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ios 远程通知(Remote Notification)和本地通知(Local Notification),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4609字,纯文字阅读大概需要7分钟。
内容图文
![ios 远程通知(Remote Notification)和本地通知(Local Notification)](/upload/InfoBanner/zyjiaocheng/1230/b0b5be40fef64d099e2d4c08b4591846.jpg)
ios通知分为远程通知和本地通知,远程通知需要连接网络,本地通知是不需要的,不管用户是打开应用还是关闭应用,我们的通知都会发出,并被客户端收到
我们使用远程通知主要是随时更新最新的数据给用户,使用本地通知主要是提醒用户来完成一些任务
远程通知 Remote Notification:
其主要的工作原理为:客户端发送自己的UUID和Bundle ID给苹果的APNs服务器-->苹果的APNs服务器加密后返回一个deviceToken给客户端-->客户端拿到devideToken后将其发送给app公司提供的服务器-->此服务器将客户端的deviceToken存储到数据库-->当服务器要发送远程通知给客户端的时候,会在数据库中拿到此客户端的deviceToken-->发送数据到苹果的APNs服务器,然后再发送到客户端
远程通知是需要真机的,另外还需要去苹果开发者中心申请证书:真机调试证书,远程推送证书(要在哪台电脑上调试或发布哪个app),描述文件证书(哪台电脑利用哪个设备调试哪个app)
我们可以使用PushMebaby来模拟服务器,也可以利用第三方软件来发送通知如Jpush等
下面是代码的实现:
1 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 2{ 3if ([UIDevice currentDevice].systemVersion.doubleValue < 8.0){ // 小于ios8 4 5 UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge; 6 7// 系统自动发送UUID和Bundle ID到苹果APNs服务器 8 [application registerForRemoteNotificationTypes:type]; 9 }else{ // 大于等于ios81011 UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound; 1213 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; 14// 通知类型15 [application registerUserNotificationSettings:settings]; 1617// 注册通知18 [application registerForRemoteNotifications]; 19 } 2021// 可以获取到userInfo数据22 NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsAnnotationKey]; 2324return YES; 25} 2627// 获得deviceToken28 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 29{ 30 NSLog(@"%@",deviceToken); 31} 3233// ios7之前调用,接收到远程通知的内容会调用 34// 程序是打开状态,不管前台还是后台,会调用这个方法 35// 如果程序是关闭状态不会调用这个,会调用application: didFinishLaunchingWithOptions:36 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 37{ 38 NSLog(@"%@",userInfo); 39} 4041// ios7之后调用,如果接收到远程通知的内容会调用这个方法42 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 43{ 44// 这个方法需要调用这个block来通知系统更新UI界面 45// UIBackgroundFetchResultNewData, 接收到数据 46// UIBackgroundFetchResultNoData, 没有接收到数据 47// UIBackgroundFetchResultFailed 接收数据失败48 completionHandler(UIBackgroundFetchResultNewData); 4950 }
本地通知 Local Notification
基本属性和方法:
属性:
- 指定通知发送的时间:NSDate *fireDate
- 指定发送通知的时区:NSTimeZone *timeZone
- 重复的周期: repeatInterval
- 通知内容:NSString *alertBody
- 锁屏状态的标题:NSString *alertAction
- 点击通知之后的启动图片:NSString *alertLaunchImage
- 收到通知播放的音乐:NSString *soundName
- 图标提醒数字:NSInteger applicationIconBadgeNumber
- 额外的信息:NSDictionary *userInfo
方法:
- 立即执行:- (void)presentLocalNotificationNow:(UILocalNotification *)notification
- 注册通知,根据指定发送时间执行:- (void)scheduleLocalNotification:(UILocalNotification *)notification
- 取消单个通知:- (void)cancelLocalNotification:(UILocalNotification *)notification
- 取消所有通知:- (void)cancelAllLocalNotifications
下面是代码实现:
1 // 创建本地通知对象 2 UILocalNotification *noti = [[UILocalNotification alloc] init]; 3 4// 指定通知发送的时间10s 5 noti.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0f]; 6// 指定时区 7 noti.timeZone = [NSTimeZone defaultTimeZone]; 8// 指定通知内容 9 noti.alertBody = @"这是通知的内容"; 1011// 设置通知重复的周期(1分钟)12 noti.repeatInterval = NSCalendarUnitSecond; 1314// 指定锁屏界面的信息15 noti.alertAction = @"这是锁屏界面的信息"; 1617// 设置点击通知进入程序时候的启动图片18 noti.alertLaunchImage = @"xxx"; 1920// 收到通知播放的音乐21 noti.soundName = @"hehe.wav"; 2223// 设置应用程序的提醒图标24 noti.applicationIconBadgeNumber = 99; 2526// 注册通知时可以指定将来点击通知之后需要传递的数据27 noti.userInfo = @{@"dogName":@"xx1", 28@"weight":@(20) 29 }; 3031// 注册添加通知32 UIApplication *app = [UIApplication sharedApplication]; 33 [app scheduleLocalNotification:noti];
注意:在ios8中需要提前注册通知类型
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2// Override point for customization after application launch. 3 4// 注意: 在iOS8中, 必须提前注册通知类型 5if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) { 6 UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound; 7 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil]; 8// 注册通知类型 9 [application registerUserNotificationSettings:settings]; 10 } 11} 1213// 接收到本地通知时就会调用,前台自动调用,后台点击通知后调用14 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 15{ 16 NSLog(@"%@",notification.userInfo); 17 }
原文:http://www.cnblogs.com/GeekStar/p/4468408.html
内容总结
以上是互联网集市为您收集整理的ios 远程通知(Remote Notification)和本地通知(Local Notification)全部内容,希望文章能够帮你解决ios 远程通知(Remote Notification)和本地通知(Local Notification)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。