zoukankan      html  css  js  c++  java
  • iOS 远程通知(Remote Notification)和本地通知(Local Notification)

      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 {
     3     if ([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{ // 大于等于ios8
    10     
    11         UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
    12         
    13         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
    14         // 通知类型
    15         [application registerUserNotificationSettings:settings];
    16         
    17         // 注册通知
    18         [application registerForRemoteNotifications];
    19     }
    20     
    21     // 可以获取到userInfo数据
    22     NSDictionary *userInfo = launchOptions[UIApplicationLaunchOptionsAnnotationKey];
    23     
    24     return YES;
    25 }
    26 
    27 // 获得deviceToken
    28 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    29 {
    30     NSLog(@"%@",deviceToken);
    31 }
    32 
    33 // ios7之前调用,接收到远程通知的内容会调用
    34 // 程序是打开状态,不管前台还是后台,会调用这个方法
    35 // 如果程序是关闭状态不会调用这个,会调用application: didFinishLaunchingWithOptions:
    36 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
    37 {
    38     NSLog(@"%@",userInfo);
    39 }
    40 
    41 // 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);
    49     
    50 }

      

      本地通知 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 = @"这是通知的内容";
    10     
    11     // 设置通知重复的周期(1分钟)
    12     noti.repeatInterval = NSCalendarUnitSecond;
    13     
    14     // 指定锁屏界面的信息
    15     noti.alertAction = @"这是锁屏界面的信息";
    16     
    17     // 设置点击通知进入程序时候的启动图片
    18     noti.alertLaunchImage = @"xxx";
    19     
    20     // 收到通知播放的音乐
    21     noti.soundName = @"hehe.wav";
    22     
    23     // 设置应用程序的提醒图标
    24     noti.applicationIconBadgeNumber = 99;
    25     
    26     // 注册通知时可以指定将来点击通知之后需要传递的数据
    27     noti.userInfo = @{@"dogName":@"xx1",
    28                       @"weight":@(20)
    29                       };
    30     
    31     // 注册添加通知
    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中, 必须提前注册通知类型
     5     if ([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 }
    12 
    13 // 接收到本地通知时就会调用,前台自动调用,后台点击通知后调用
    14 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    15 {
    16     NSLog(@"%@",notification.userInfo);
    17 }

      

    欢迎加QQ群交流: iOS: 279096195 React Native: 482205185
  • 相关阅读:
    [Swift通天遁地]三、手势与图表-(9)制作五彩缤纷的气泡图表
    hdu2289 Cup(二分)
    Makefile学习(三)[第二版]
    CABasicAnimation 基本动画
    iOS_20_微博自己定义可动画切换的导航控制器
    yispider 开源小说採集器 (来源http://git.oschina.net/yispider/yispider 我的改动版由于他的我无法跑)
    谈谈C++私有继承
    深入struts2.0(七)--ActionInvocation接口以及3DefaultActionInvocation类
    STL 之 list源码自行实现(iterator)
    二分lower_bound()与upper_bound()的运用
  • 原文地址:https://www.cnblogs.com/GeekStar/p/4468408.html
Copyright © 2011-2022 走看看