为更好的处理远程推送提供一种可能
SGPush 将处理远程推送时所需的头文件引用、逻辑代码封装到 SGPushHandler 的子类中,当点击远程推送消息打开应用或应用开启状态下接收到远程推送消息时自动执行相应的处理逻辑。
类 | 描述 |
---|---|
SGPush | 核心类,管理注册的SGPushHandler 子类,接管系统接收到的远程推送消息 |
SGPushHandler | 推送数据以及相应处理逻辑的封装类,请根据具体业务需求创建不同的子类并在executeFromViewController: 方法中实现推送处理逻辑,该类本身不可直接使用。 |
UIViewController+SGPush | 提供canHandleRemotePush: 方法,不同的视图控制器可根据需求选择是否处理推送消息 |
每一个子类实现一种远程推送类型的处理逻辑,在子类中重载 executeFromViewController:
方法实现推送处理逻辑。
这里我们假定实现了 MessagePushHandler
子类来处理用户的消息提醒,DefaultPushHandler
子类来为所有其他类型的推送提供默认处理。
想要实现的子类发挥作用需要将它注册到 SGPush 中并与接收到的推送字典 userInfo 中的特定字段和值做绑定。这样当接收到的推送消息含有特定的字段或值时,SGPush 会调用执行相应的 SGPushHandler 子类处理逻辑。
在 application:didFinishLaunchingWithOptions:
方法中进行子类注册:
[SGPush registerPushHandlerClass:[MessagePushHandler class] forKeyPath:@"aps.messageId"];
[SGPush registerPushHandlerClass:[DefaultPushHandler class] forKeyPath:@"aps"];
在 application:didFinishLaunchingWithOptions:
中加入代码:
[SGPush handleRemotePush:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];
在 application:didReceiveRemoteNotification:
中加入代码:
[SGPush handleRemotePush:userInfo];
通过以上几部,当服务器向设备发送的推送内容如下时
{"aps":{"alert":"This is a test message.","messageId":100}}
SGPush 会创建 MessagePushHandler 对象并执行。若推送内容中没有 messageId 字段时,SGPush 会创建 DefaultPushHandler 对象执行推送处理逻辑。
通过 SGPush 还可以实现更多灵活的推送处理方式,请参考 Demo 中的注释和 SGPush 源码注释。