iOS工程实践

iOS APP 推送终结版

2017-05-13  本文已影响716人  奔哥小木屋

这篇文章我同事总结的, 感谢茗荚小草, 以下为原文.


iOS 7 - 基本的通知推送
iOS 8 - 通知+自定义button
iOS 9 - 通知+自定义button+快捷回复

使用Houston在本地调试远程通知

安装houston

gem install houston

安装完之后

apn push "<19904e80 0ab7f3e7 bc59ba36 3fda26f3 f4c69568 81cecc7f 4c675143 ba6fc83d>" -c /Users/bjhl/Desktop/apppush_for_CRM/apple_push_notification.pem -m "hello" -b "15"  -d key=CRM -y actionCategory

-c 指定.pem文件的地址,-m 通知的文本内容 ,-b 角标的数量, -y category的标识符
要发送静默通知,.pem 文件之后,只添加-n 即可。-n 代表content-availabel
"<>"中的string为deviceToken。

在使用之前首先需要准备好pem证书文件

Push Services证书

上述准备工作做好之后,xcode运行

通过didRegisterForRemoteNotificationsWithDeviceToken 获取deviceToken

app处理接收到的通知

根据收到通知时,app的不同状态,app的处理方式不同

iOS8中推送代理方法只有两个:

iOS 10中推送:UNUserNotificationCenterDelegate

可交互的通知

iOS8拥有了全新的通知中心,有全新的通知机制。当屏幕顶部收到推送时只需要往下拉,就能看到快速操作界面,并不需要进入该应用才能操作。在锁屏界面,对于推送项目也可以快速处理。基本上就是让用户尽量在不离开当前页面的前提下处理推送信息,再次提高处理效率。
app不在前台时,出现推送通知,下拉或者3D touch 该通知都可以出现自定义的操作。

image
一般的远程推送消息 payload最大为2kb,256字节,超过该限制,APNS拒绝转发。

payload实际是一个JSON字典

初始化

要使用自定义的action,需要先判断当前系统为8.0+,再使用UIUserNotificationSettings,UIMutableUserNotificationCategory,UIMutableUserNotificationAction,自定义通知的操作。

         UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        //UIUserNotificationActivationModeBackground 不会调起app到前台,在后台处理
        //UIUserNotificationActivationModeForeground 点击该按钮时启动app到前台,且当前模式下setAuthenticationRequired默认为YES,
        [action1 setActivationMode:UIUserNotificationActivationModeBackground];
        [action1 setTitle:@"Background"];//展示在通知上的title
        [action1 setIdentifier:@"action1"];//action的唯一标识
        [action1 setDestructive:YES];//当前操作按钮为红色
        [action1 setAuthenticationRequired:YES];//需要解锁与否
        ……
        action2, action3, action4, action5, action6, action7
        UIMutableUserNotificationCategory *actionCategory = [[UIMutableUserNotificationCategory alloc] init];
        [actionCategory setIdentifier:@"actionCategory"];//这组动作的标识,同后台约定
        [actionCategory setActions:@[action1, action2, action3, action4, action5, action6, action7] forContext:UIUserNotificationActionContextDefault];
        //使用UIUserNotificationActionContextDefault没有action数目限制,但是action过多屏幕太短也无力……,UIUserNotificationActionContextMinimal不展示action
        NSSet *category = [NSSet setWithObject:actionCategory];
        UIUserNotificationType types = (UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:category];
        
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

iOS8以下的系统,注册远程通知:

       [[UIApplication sharedApplication] registerForRemoteNotificationTypes:  
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"application:didReceiveRemoteNotification:fetchCompletionHandler");

    //TODO:对远程通知的处理

    if(completionHandler) {
        completionHandler(UIBackgroundFetchResultNoData);
    }
}
    
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    NSLog(@"handleActionWithIdentifier:forRemoteNotification");
        //TODO:对远程通知的处理,用户tap了identifier匹配的action之后的操作

    if(completionHandler) {
        completionHandler();
    }
}

通知中心的快速回复 iOS 9.0–10.0Deprecated

以上为点击通知的按钮,进行操作,iOS9增加了一个供用户快速回复的Category类型
UIUserNotificationActionBehaviorTextInput(iOS 9.0–10.0Deprecated

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) {
            //iOS 支持在通知中心快速回复输入文字
            UIMutableUserNotificationAction *inputAction = [[UIMutableUserNotificationAction alloc] init];
            inputAction.behavior = UIUserNotificationActionBehaviorTextInput;
            inputAction.activationMode = UIUserNotificationActivationModeBackground;
            [inputAction setTitle:@"inputAction"];
            [inputAction setIdentifier:@"inputAction"];
            [inputAction setDestructive:NO];
            //设置发送键的title
            inputAction.parameters = @{UIUserNotificationTextInputActionButtonTitleKey:@"哇塞"};
        }
 - (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler; 

方法中的responseInfo中的UIUserNotificationActionResponseTypedTextKey对应的value来获取输入的文字。

上一篇下一篇

猜你喜欢

热点阅读