美文网首页
iOS10-推送(本地和远程)的简单使用

iOS10-推送(本地和远程)的简单使用

作者: shenkaiqiang | 来源:发表于2016-12-21 09:18 被阅读475次

参考文章:
一、iOS10 关于推送
二、iOS10通知框架UserNotification理解与应用

注:此文只能适配iOS10及以上的系统,如果需要适配iOS9及以下的系统可读下面的文章。
iOS8的推送方法可以查看:iOS8-推送(本地和远程)的简单使用

在iOS10中,把远程推送和本地推送整合到了一起,代理方法也是相同的了。所以就一起介绍了。
首先需要添加框架 UserNotifications.framework
(#import <UserNotifications/UserNotifications.h>)
还要遵守通知的代理协议 <UNUserNotificationCenterDelegate>

推送的注册

// 同样是在下面的这个方法中注册
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //进行用户权限的申请
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self; // 遵守代理协议
    [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
          //在block中会传入布尔值granted,表示用户是否同意
          if (granted) {
                  //点击允许
                  NSLog(@"注册通知成功");
          } else {
                //点击不允许
                 NSLog(@"注册通知失败");
          }
    }];

    // 如果是注册远程推送,还是和以前一样,需要加上下面这行代码
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    return YES;
}

获取用户对通知的设置信息 的方法也不再是原来的方法了。
iOS10以前是一个代理方法:

/** 获取用户对通知的设置信息 */
//NS_AVAILABLE_IOS(8_0);
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  {
    NSLog(@"setting=== %@",notificationSettings);
}

iOS10之后变成了一个实例方法,上面的代理方法不会运行:

[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        NSLog(@"%@", settings);
 }];

关于注册远程通知的回调方法还是和以前一样:

/** 远程通知注册成功委托 */ //NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  {
    // 对 deviceToken 进行处理
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    // 需要把 deviceToken 上传给服务器
    /**上传deviceToken的代码*/
}
/** 远程通知注册失败委托 */ //NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  {
  NSLog(@"%@",error);
}

接下来是iOS10中收到推送通知的方法回调,也是在日常使用中和之前完全不一样的地方。
在iOS10之前的系统中,本地推送通知和远程推送的收到推送通知的方法回调是分开的,而且如果携带的 Category参数的话,回调方法也是和没有携带参数的方法不同。
但是在iOS10中,无论是本地推送通知还是远程推送通知,无论有没有携带 Category参数,调用的方法都是一样的:

#pragma mark --- iOS 10.0
// 程序在前台的时候对通知的处理。
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
    NSLog(@"=== willPresentNotification");
}
// 程序在后台但是点击了收到的推送通知打开程序
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    NSLog(@"=== didReceiveNotificationResponse");
    //处理完消息,最后一定要调用这个代码块
    completionHandler();
}

获取和删除通知
这里通知有两种状态:
Pending 等待触发的通知
Delivered 已经触发展示在通知中心的通知

//获取未触发的通知
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
    NSLog(@"pending: %@", requests);
}];

//获取通知中心列表的通知
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
    NSLog(@"Delivered: %@", notifications);
}];

 //清除某一个未触发的通知
 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]];
  //清除某一个通知中心的通知
 [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]];
  //对应的删除所有通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];

创建普通的本地通知

//通知内容类
    UNMutableNotificationContent * content = [UNMutableNotificationContent new];
    //设置通知请求发送时 app图标上显示的数字
    content.badge = @2;
    //设置通知的内容
    content.body = @"这是iOS10的新通知内容:普通的iOS通知";
    //默认的通知提示音
    content.sound = [UNNotificationSound defaultSound];
    //设置通知的副标题
    content.subtitle = @"这里是副标题";
    //设置通知的标题
    content.title = @"这里是通知的标题";
    //设置从通知激活app时的launchImage图片
    content.launchImageName = @"lun";
    //设置5S之后执行
    UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
    UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
    //添加通知请求
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        
    }];
push.png

iOS10的新特性

以上就是关于iOS10推送的简单使用。当然iOS10的推送相比以前的功能更加丰富了,比如:
1.UserNotification支持自定义通知音效和启动图。
2.UserNotification支持向通知内容中添加媒体附件,例如音频,视频。
3.UserNotification支持开发者定义多套通知模板。
4.UserNotification支持完全自定义的通知界面。
5.UserNotification支持自定义通知中的用户交互按钮。
关于这些功能,因为目前暂时没有用到,可以查看我上面列出的参考文章:
iOS10通知框架UserNotification理解与应用

相关文章

  • iOS10-推送(本地和远程)的简单使用

    参考文章:一、iOS10 关于推送二、iOS10通知框架UserNotification理解与应用 注:此文只能适...

  • swift 消息推送

    消息推送本身不难,基本分为远程推送(包含静默推送,特殊的远程推送)和本地推送;在实际项目中基本都会使用到,比较头痛...

  • iOS本地通知和远程推送

    iOS 本地通知和远程推送 推送通知的应用,可以推送最新的消息给用户,获得更多的关注。推送分为本地推送和远程推送两...

  • iOS远程推送之(一):APNs原理和基本配置

    一、前言 iOS中消息推送有两种方式,本地推送和远程推送。本地推送在iOS中使用本地通知为你的APP添加提示用户功...

  • 本地推送通知、远程推送通知、激光推送

    title : 本地推送通知、远程推送通知、激光推送category : UI 本地推送通知、远程推送通知、激光...

  • 一些项目中可能只需要配置一次的东西吧

    本地推送&远程推送 1.远程推送 推送通知的分类远程推送通知本地推送通知 推送通知作用可以让不在前台运行的App告...

  • ios voip 推送

    什么是voip推送和普通的ios推送有什么区别呢?普通推送分为:远程推送和本地推送,区别网上资料太多了,简单说一下...

  • iOS 关于远程推送的知识普及

    首先简单介绍一下iOS下推送通知吧!,推送通知分为有local本地推送,remote远程推送,而iOS7之后对远程...

  • 本地通知和远程推送

    推送分为本地推送和远程推送,远程推送要求用开发者账号,一般使用极光推送,过程比较繁琐,需要用开发者账号在苹果开发者...

  • iOS远程推送

    iOS推送分为远程推送 和 本地推送两种,本地推送此文章先不说,之后会有新的文章更新。远程推送将一些重要的信息推送...

网友评论

      本文标题:iOS10-推送(本地和远程)的简单使用

      本文链接:https://www.haomeiwen.com/subject/rwtqvttx.html