iOS 推送设置及相应处理

作者: XPorter | 来源:发表于2017-02-28 17:18 被阅读2105次

推送设置

1.推送界面设置

在iOS8 之后,推送可以设置一些操作按钮,即使app在后台甚至是app被杀掉的情况下也能做一下简单的操作,就像微信的快捷回复一样。

  1. 设置action
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
    action1.identifier = @"action1";
    action1.title=@"忽略";
    action1.behavior = UIUserNotificationActionBehaviorDefault; // 操作类型,默认按钮
    action1.activationMode = UIUserNotificationActivationModeBackground; // 后台操作,不需要打开app
    action1.authenticationRequired = YES; // 是否需要解锁
    action1.destructive = YES; // 样式区别

    UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
    action2.identifier = @"action2";
    action2.title=@"回复";
    action2.behavior = UIUserNotificationActionBehaviorTextInput; // 操作类型,输入框
    action2.activationMode = UIUserNotificationActivationModeBackground; // 后台操作,不需要打开app

    UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
    action3.identifier = @"action3";
    action3.title=@"查看";
    action3.behavior = UIUserNotificationActionBehaviorDefault; // 操作类型,默认按钮
    action3.activationMode = UIUserNotificationActivationModeForeground; // 前台操作,打开app
//如果 activationMode = UIUserNotificationActivationModeForeground,authenticationRequired设置之后不起作用;
//因为如果需要前台操作就需要打开app,那肯定是需要解锁的
    action1.authenticationRequired = NO;
  1. 组装category
// 可以设置多种样式,在推送的时候通过字段category控制样式
//category1
UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
category1.identifier = @"Category1";
[category1 setActions:@[action1,action2,action3] forContext:(UIUserNotificationActionContextDefault)];
//category2
UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];
category2.identifier = @"Category2";
[category2 setActions:@[action2] forContext:(UIUserNotificationActionContextDefault)];
  1. 设置推送
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings: uns];

2.推送红点数设置

对于BadgeNumber,只是一种提醒方式,在系统的通知设置页面可以对相应的app设置显示样式,其中就有一个选项是是否显示图标标记。
对于BadgeNumber的值,在推送消息中用badge来设置,客户端可以用[UIApplication sharedApplication].applicationIconBadgeNumber对红点数进行修改。并且每当有推送过来的时候,显示的BadgeNumber都会跟推送中设置的值一致,不会自动累加。

3.推送声音设置

推送声音的音频文件,试了aiff,wav,caf,mp3格式的音频貌似都可以。
将音频文件添加到项目中之后,就可以通过sound字段来控制推送的声音。

4.推送内容设置

一般来说,推送显示的内容可以通过alert字段进行直接的设置。同时对于一些固定格式的推送内容,可以配合Localizable.strings来设置。在Localizable.strings可以设置一条信息:

"weather" = "今天天气为%@,温度%@度";

推送内容设置为:

"alert":{"loc-args":["晴","15"],"loc-key":"weather"}

5.静默推送

需要设置"content-available" : 1,既然是静默推送就是不让用户感知,所以alert和sound字段一般都不设置。如果设置了,就会有提示,但本质上讲还是静默推送,因为它让然会默默的调用下面的方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;

推送处理

一般的推送处理,就是点击推送的消息打开app,如果要增加体验的话可以在点击推送之后做一下操作,比如跳转页面或者刷新数据。
app在不同的状态下,点击推送会触发不同的方法
1、app前台运行,这条推送不会出现在消息栏,但是会触发下面的方法。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;

2、后台运行,点击推送消息,包括通知栏和顶部提示,启动app
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo ;

4、进程已经被kill,点击推送启动app,launchOptions 有值,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

5、推送有操作按钮,微信的锁屏回复就是通过这个方法处理的
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler;

6、静默推送,收到静默推送时,会主动调用下面的方法,不需要操作

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler ;

推送内容格式

  1. 一般形式
{
      "aps":{
              "alert":"message",
              "badge":1,
              "category":"Category2",
              "sound":"Glass.aiff"
      }
}
  1. 本地拼接推送内容
{
      "aps":{
              "alert":{
                     "loc-args":["晴","15"],
                      "loc-key":"weather"
               },
              "badge":1,
              "category":"Category2",
              "sound":"Glass.aiff"
      }
}
  1. 静默推送
{
      "aps":{
              "alert":{
                     "loc-args":["晴","15"],
                      "loc-key":"weather"
               },
              "badge":1,
              "category":"Category2",
              "sound":"Glass.aiff",
              "content-available" : 1
      }
}

搞了个推送工具方便测试
可以通过scheme跳转的方式添加token
添加证书方式为p12文件的base64字符串

相关文章

  • iOS 推送设置及相应处理

    推送设置 1.推送界面设置 在iOS8 之后,推送可以设置一些操作按钮,即使app在后台甚至是app被杀掉的情况下...

  • flutter 远程推送

    iOS 如果要在后台的时候收到远程推送,需要设置background modes 如果要处理后台收到的推送,推送消...

  • iOS跳转到相应的权限

    iOS开发如何实现跳转到相应权限设置里 注意:iOS8及以后调用如下方法: NSURL *url = [NSURL...

  • iOS 远程推送通知

    iOS 远程推送通知 分分钟搞定IOS远程消息推送 iOS推送通知的实现步骤 推送通知iOS客户端编写实现及推送服...

  • iOS 推送参考文档

    1、ios 消息推送证书设置和整理(备忘)2、iOS 远程推送APNS从0至发布-极光推送& 真机测试篇3、iOS...

  • APNS消息推送的实现(完整步骤)

    1. 原理及代码实现 iOS远程推送原理及实现过程 苹果远程推送通知 APNs 详解,官方,iOS | Swift...

  • iOS 推送

    iOS 下APNS推送处理函数详解

  • iOS-iOS10极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • iOS-极光推送的使用

    1、首先先配置好推送证书,传到极光。极光推送->iOS证书设置指南极光推送->iOS SDK集成指南(XCode8...

  • ios 推送处理

    ios推送分为3种情况 1. 当APP为关闭状态时,点击通知栏消息跳转到指定的页面 2. 当APP在后台运行时,点...

网友评论

本文标题:iOS 推送设置及相应处理

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