刚出来工作的时候就接触到极光推送相关的知识了,但是之前业务对推送消息的处理深度有限。没有做过多的研究,近来对其重新进行了一下研究,暂记录如下,后面有机会的话会更深入的进行探索。此代码希望能够帮助刚接触极光推送的同学对推送有个大致了解,也可拷贝进行使用。有什么好的建议或者不足,可给我留言。
推送大致可以分为三种:
1、APP前台运行时收到推送消息
2、APP后台运行(或还没有完全被杀死)收到推送消息
3、APP没有运行时收到推送消息
以下代码对这三种推送消息接收的地方都进行了处理。因为这一部分代码数量相对较多,放到AppDelegate里面的话,会使AppDelegate类显得臃肿,固用类别的方式进行管理。在AppDelegate中使用的地方进行相关调用即可。
AppDelegate.m
//配置极光,如果通过消息启动,并进行操作
[self Jpush_application:application didFinishLaunchingWithOptions:launchOptions];
AppDelegate+Jpush.h
#import "AppDelegate.h"
#ifdef DEBUG
static BOOL isProduction = NO;
#else
static BOOL isProduction = YES;
#endif
static NSString *appKey = @"*********";
static NSString *channel = @"Publish channel";
@interface AppDelegate (Jpush)
- (BOOL)Jpush_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end
AppDelegate+Jpush.m
#import "AppDelegate+Jpush.h"
#import "JPUSHService.h"
//#import "NSObject+commonuse.h"
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
@implementation AppDelegate (Jpush)
- (BOOL)Jpush_application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[JPUSHService setLogOFF];//setDebugMode
//推送配置,及注册服务
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//获取registrationID 项目服务器发推送需要
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
//upload registrationID to server
}];
//启动推送服务
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction];
//根据APP启动参数,判断是否由推送消息唤醒APP,并对消息进行处理
if (launchOptions) {
NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
//此处将推送消息保存在通知参数内。可在首页加载完成添加监听,实现跳转等操作。可能有更好的办法
[self performSelector:@selector(sendNotificationInfoToHomeVC:) withObject:remoteNotification afterDelay:1];
}
}
return YES;
}
#pragma mark --- 注册deviceToken到JPUSHService
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//向JPUSHService 上传deviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"推送注册失败");
}
#pragma mark --- APP 前台运行/后台运行 收到消息都会先走此方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark --- 程序前台运行中 调用此方法
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
//对消息进行处理 比如自定义弹框显示在当前window上面
// NSLog(@"前台收到推送:%@",[self getCurrentWindow]);
}
completionHandler(UNNotificationPresentationOptionAlert);
}
#pragma mark --- 程序在后台,点击消息 调用此方法
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
//对消息进行处理 比如从当前控制器 push or present
// NSLog(@"通过消息APP从后台被唤醒:%@",[self getCurrentViewController]);
}
completionHandler();
}
#pragma mark --- app events
- (void)sendNotificationInfoToHomeVC:(id)dict {
[[NSNotificationCenter defaultCenter]postNotificationName:@"SystemMsgCallApp" object:dict];
}
接收到消息后的处理:推送的消息一般最终都会出现在消息中心的模块中。在项目中对所有的消息类型进行了一个判断,系统消息、订单消息、聊天消息等等。每种消息对应的相关操作都做了相关的处理。保证点击消息会跳转到相关界面(原生界面、html)等。
网友评论