iOS10推送开发

作者: AryCode | 来源:发表于2016-09-30 17:34 被阅读856次
    • iOS10推送授权
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
        // 异步回调,不会阻塞UI
        if (granted)
        {
            //授权成功
        }
        
        if (!granted || error)
        {
            //授权失败
        }
    }];```
    
    * 获取授权结果,这样方便以后我们定制推送信息展现方式
    
    ```sh
    [[UNUserNotificationCenter currentNotificationCenter]  getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
    }];
    

    UNNotificationSettings属性

    UNAuthorizationStatus authorizationStatus;
    
    UNNotificationSetting soundSetting __TVOS_PROHIBITED;
    UNNotificationSetting badgeSetting __WATCHOS_PROHIBITED;
    UNNotificationSetting alertSetting __TVOS_PROHIBITED;
    
    UNNotificationSetting notificationCenterSetting __TVOS_PROHIBITED;
    UNNotificationSetting lockScreenSetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
    UNNotificationSetting carPlaySetting __TVOS_PROHIBITED __WATCHOS_PROHIBITED;
    
    UNAlertStyle alertStyle;
    
    • iOS10本地推送示例
    • 定义推送
    // 推送内容,其中UNMutableNotificationContent 和10以前的UILocalNotification作用类似
    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"title" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"body"                                        arguments:nil];
    content.sound = [UNNotificationSound defaultSound];
    // 定义触发推送的触发器
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                          triggerWithTimeInterval:3 repeats:NO];
    // 还有一些其它的触发器可以在官网查看,接下来发送推送
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecondIdentifier"
                                                                                  content:content trigger:trigger];
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            NSLog(@"推送请求处理完毕");
    }];
    

    以上就是一个简答的本地推送示例

    • iOS10处理推送过来的消息
      值得一提的是,iOS10容许应用在前台的情况下,展现推送信息,以前当我们应用在前台如果来了一个推送,一般都是一个AlertView供用户选择查看,不过在iOS10以后,推送可以在前端展示,不过需要实现如下的代理,并加入枚举值
      UNNotificationPresentationOptionAlert
    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
    {
        // last involve
        completionHandler( UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
    }
    

    这个消息仅应用在前台的时候会显示,当我们点击这个推送本身,或者,点击推送下面的action按钮,将会触发下面的代理方法

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
    {
        completionHandler();
    }
    

    你可以根据推送的response如何处理这个点击事件

    iOS10推送新特性

    • iOS10容许携带多媒体信息,比如图片,视频,这个特性,是由一个UNNotificationAttachment类实现的,下面是一个携带图片的示例
    IMG_3196.PNG
    当我们下拉,或者使用3Dtouch功能,推送会变成这样 IMG_3197.PNG

    以下是完整的代码

    UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
    content.title = [NSString localizedUserNotificationStringForKey:@"套路" arguments:nil];
    content.body = [NSString localizedUserNotificationStringForKey:@"编程就是个套路!"
                                                         arguments:nil];
    content.categoryIdentifier = @"categoryIdentifier";
    content.sound = [UNNotificationSound defaultSound];
    
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"IMG_3162" withExtension:@"png"];
    UNNotificationAttachment * attach = [UNNotificationAttachment attachmentWithIdentifier:@"attachmentWithIdentifier" URL:url options:nil error:nil];
    content.attachments = @[attach];
    
    // Deliver the notification in five seconds.
    UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger
                                                  triggerWithTimeInterval:3 repeats:NO];
    UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                          content:content trigger:trigger];
    
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"推送处理完毕");
    }];
    
    UNTextInputNotificationAction * textAction = [UNTextInputNotificationAction actionWithIdentifier:@"FiveSecond" title:@"评论" options:UNNotificationActionOptionForeground textInputButtonTitle:@"评论" textInputPlaceholder:@"占位文字"];
    
    UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"cancel" title:@"删除" options:UNNotificationActionOptionDestructive];
    
    UNNotificationCategory * identify = [UNNotificationCategory categoryWithIdentifier:@"categoryIdentifier" actions:@[textAction,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionAllowInCarPlay];
    [center setNotificationCategories:[NSSet setWithObject:identify]];
    

    注意: 这是当应用在前台的时候显示的,所以要实现上面的协议

    • 上面的推送时本地的,如果是远程推送的话,需要实现搭建以下APP Extend来实现多媒体推送
    BF531B98-5382-4561-A170-6CE9B0BD02CB.png
    假设如你的推送信息如下
    {
      "aps":{
        "alert":{
          "title":"多媒体携带图片显示",
          "body":"会将图片下载下来,在现实这个通知"
        },
        "mutable-content":1
      },
      "image": "http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"
    }
    

    那么考虑到推送过来的信息仅仅只有一个图片的链接,地址,该如何显示,幸运的是iOS10通过Server Extend可以拦截推送过来的信息,在收到这种需要进一步处理的推送信息,系统有个30s钟的时间给你处理,处理的函数在你新建的Server Extend NotificationService类的以下方法中

    - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
        self.contentHandler = contentHandler;
        self.bestAttemptContent = [request.content mutableCopy];
        
        // 此处做一些你对这个推送想要做的一些更改,比如根据图片链接下载图片...
        self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
        // 这句话一定要在你加工完毕推送信息后调用
        self.contentHandler(self.bestAttemptContent);
    }
    

    如果30s以后仍然没有加工完毕,怎么办?苹果给了我们一个一个补救办法

    - (void)serviceExtensionTimeWillExpire {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        self.contentHandler(self.bestAttemptContent);
    }
    

    通过这个方法,我们可以做一些简单的修改,或者直接显示原本的推送内容

    那么如何触发这样的推送消息
    推送消息中要携带"mutable-content":1告诉系统,这个推送需要进一步加工

    未完...

    相关文章

      网友评论

        本文标题:iOS10推送开发

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