关于JPush
极光推送是国内的服务厂商提供的一站式push服务(同时支持iOS、Android),后面也加入了即时通讯的能力供app使用。致力于打造简单、可靠、价格有竞争力的服务(简单功能全免费,高级版才收费),让应用开发商可以聚焦业务开发,push相关的技术实现全部通过极光推送来解决,仅需调用极光推送的api即可。正因为如此,开发者小伙伴们对其的评价相当不错。笔者的app新增了从服务器往移动客户端下发简单消息的需求,多方权衡下,我们准备尝试jpush的服务来解决问题(这里仅介绍iOS端的集成过程)。
集成步骤
step1:去极光推送注册账号:https://www.jpush.cn/,并注册应用。
step2:上传apns证书到极光,apns证书的生成步骤参考:
http://docs.jpush.io/client/ios_tutorials/#ios_1
仔细阅读该文档,上传成功后控制台的应用详情里面会显示“已验证”,说明证书有效。
step3:对你的app工程重新配置,使用新的支持apns的provision文件(若此前应用已支持apns,可以不用换),否则后面无法正常获得device token.
step4:集成其sdk,包括一个.a和一个.h文件,最新版本大约是2.1,其sdk中也包含了demo,注意在2.1版本之前需要创建一个plist文件用于保存秘钥信息等,在最新版本不需要此文件。
sdk下载地址:https://www.jpush.cn/common/products#product-download
step5:主要代码修改如下:
appdelegate.h:
static NSString *appKey = @"Your_app_key";static NSString *channel = @"Your_channel_identifier";static BOOL isProduction = NO;
appdelegate.m:
#import "JPUSHService.h"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; [JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction]; return YES;}- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]); [JPUSHService registerDeviceToken:deviceToken];}- (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfo{ NSLog(@"didReceiveRemoteNotification :%@", userInfo); [JPUSHService handleRemoteNotification:userInfo];}- (void)application:(UIApplication *)applicationdidReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ NSLog(@"didReceiveRemoteNotification fetchCompletionHandler:%@", userInfo); [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData);}- (void)application:(UIApplication *)applicationdidReceiveLocalNotification:(UILocalNotification *)notification{ }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
监听自定义消息相关的通知(建议在你应用中创建一个单例的manager中专门管理):
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(networkDidSetup:) name:kJPFNetworkDidSetupNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidClose:) name:kJPFNetworkDidCloseNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidRegister:) name:kJPFNetworkDidRegisterNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidLogin:) name:kJPFNetworkDidLoginNotification object:nil]; [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil]; [defaultCenter addObserver:self selector:@selector(serviceError:) name:kJPFServiceErrorNotification object:nil];- (void)networkDidSetup:(NSNotification *)notification{ NSLog(@"networkDidSetup %@, alias=%@", [notification userInfo], [NSString stringWithFormat:@"iOS_%@", GSignInConfig.userID]); [JPUSHService setTags:nil alias:[NSString stringWithFormat:@"iOS_%@", GSignInConfig.userID] callbackSelector:nil target:nil];}- (void)networkDidClose:(NSNotification *)notification{ NSLog(@"networkDidClose %@", [notification userInfo]);}- (void)networkDidRegister:(NSNotification *)notification{ NSLog(@"networkDidRegister %@", [notification userInfo]);}- (void)networkDidLogin:(NSNotification *)notification{ NSLog(@"networkDidLogin %@", [notification userInfo]);}- (void)networkDidReceiveMessage:(NSNotification *)notification{ NSLog(@"networkDidReceiveMessage notification = %@", notification);}- (void)serviceError:(NSNotification *)notification{ NSDictionary *userInfo = [notification userInfo]; NSString *error = [userInfo valueForKey:@"error"]; NSLog(@"serviceError %@", error);}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
api参考:http://docs.jpush.io/client/ios_api/#api-ios
step6:在控制台发送消息,对其进行测试。
https://www.jpush.cn/push/apps/61466f6a310571060af61a13/push/message/
在此处可以发送自定义消息和push消息,其中自定义消息只在前台接收,通过长连接发送。笔者这里只使用自定义消息,用于应用在前台时服务器向应用下发消息,实时性比较好。
错误排查
http://docs.jpush.io/client/ios_tutorials/
参考上面链接的思维导图,结合demo(将demo的bundle id修改成你工程的bundle id,并配置你自己的mobile provision文件来对比测试),一般就可以解决常见问题。
总结
经过笔者几个小时的实战,即在应用内集成了Jpush的服务,应该说还是相当效率的。Jpush在速度、可靠性、文档等各方面表现都很不错,对开发者很友好。有此类需求的开发者小伙伴们可以优先考虑JPush的方案。
0 0