iOS 集成腾讯信鸽推送 XGPush

    xiaoxiao2021-12-12  11

    本来项目用的JPush,因为安卓那边的问题进程被kill之后无法接收到推送,于是改嫁到腾讯XGPush,用这个的还蛮少的,所以就写一篇博客分享给用这个的朋友。

    强调本篇文章是开发环境~

    其实官方的Demo是很好用的,但是有些地方很容易造成人误解···

    比如我入的这个坑,运行代码后Console报错说我的设备注册失败!

    可是我用信鸽推送助手,如图在这个位置找到下载下来。

    icon长这个样子,如图

    打开它上传我的.cer针对我的devicTokenStr推送

    然后push出去显示这个提示框,别在意这个,去看你的手机是否收到推送才是正确的推送结果(可能有延迟,1min到几小时不等~)

    我的手机亮了起来,显示已经收到了推送,这说明了什么???说明了我的deviceToken是注册成功的,为什么回调的方法告诉我注册失败???

    既然这样,我setAccount也应该成功了,为什么我定向推送到这个账号上显示的却是无效账号?

    我是按照Demo里的步骤一步步来的,为什么是这样的结果???

    蓝瘦,香菇,如图是Demo里的代码,不可以磨灭的证据,证明我的中规中矩,一点儿也没越界···

    但是为什么我的deviceToken注册成功了我的account就无效呢?

    宝宝我带着无数问好点进了setAccount的方法里···

    what???

    那开发者中心的setAccount是怎么回事???

    苍天啊,大地啊,为什么???为什么要这样对我,都怪我这么天真可爱····

    然后,我决定改个Account名称试试····

    运行起来

    此时我的内心是崩溃的···

    我本来代码没有错,硬是让我出错的情况,让我精神崩溃了一上午,连午饭都无心去吃···

    宝宝心里苦。

    好吧,言归正传。

    1.下载证书

    主要流程如图:

    跟着官方文档开始一步一步来~

    首先,登录苹果开发者中心网站。然后点击Certificates,Identifiers & Profiles

    然后点击Certificates

    选中需要制作Push证书的应用,勾选Push服务

    下面以制作开发证书为例演示。点击Create Certificate…

    然后打开Keychain Access工具。

    选择Request a Certificate From a Certificate Authority…

    填写邮件地址,其它留空, 继续。会将证书保存到本地。

    返回网站,选择刚才创建的文件上传。

    成功后,下载到本地

    再次打开Keychain Access。选中Push证书导出,选中一行。导出的格式为p12。

    生成pem格式的证书  


    完成上述操作后,打开终端,进入到p12文件所在执行以下命令。

    1 openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes

    则生成了CertificateName.pem证书,上传到信鸽则可以进行消息推送。

    2.创建应用获取AccessID和AccessKey并上传验证证书

    上传.pem证书即可~ 3.下载SDK,导入到工程中 获取Access ID获取Access Key

    前往信鸽官网 点击打开链接 ,用QQ号登录,添加应用基本信息并注册,之后可获取Access ID和Access Key。

    工程配置

    1、 下载信鸽SDK压缩包到本地并解压;

    2、 创建或打开Xcode iOS工程;

    3、 将XGSetting.h 和 XGPush.h 和 libXG-SDK.a添加到Xcode工程

    4、 添加对以下libraries的引用。包括CFNetwork.framework , SystemConfiguration.framework , CoreTelephony.framework , libz.dylib , libXG-SDK.a,Security.framework

    4.工程配置

    配置好所需要的team和provisioning profile

    打开相应的开关

    5.代码

    #import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #define _IPHONE80_ 80000 @interface AppDelegate () @property (nonatomic, retain) NSDictionary *remoteNotification; @property (nonatomic, assign) BOOL isLaunchedByNotification; @property (nonatomic, assign) BOOL isBackGround; @end @implementation AppDelegate - (void)registerPushForIOS8{ //Types UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; //Actions UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; acceptAction.identifier = @"ACCEPT_IDENTIFIER"; acceptAction.title = @"Accept"; acceptAction.activationMode = UIUserNotificationActivationModeForeground; acceptAction.destructive = NO; acceptAction.authenticationRequired = NO; //Categories UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init]; inviteCategory.identifier = @"INVITE_CATEGORY"; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; [inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextMinimal]; NSSet *categories = [NSSet setWithObjects:inviteCategory, nil]; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } - (void)registerPush{ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //初始化push信息 [XGPush startApp:2200242228 appKey:@"I45EUD1Z26JJ"]; [self managerPush:launchOptions]; //获取推送的信息字典 _remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (_remoteNotification) { _isLaunchedByNotification =YES;//用来判断是否是点击推送进入项目 }else { _isLaunchedByNotification =NO; } return YES; } - (void)managerPush:(NSDictionary *)launchOptions { void (^ successCallback)(void) = ^(void) { if (![XGPush isUnRegisterStatus]) { //iOS8之前需要注册状态 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue]; if (sysVer < 8) { [self registerPush]; } else { [self registerPushForIOS8]; } #else [self registerPush]; #endif } }; [XGPush initForReregister: successCallback]; //app不在运行时,点击推送激活 [XGPush handleLaunching:launchOptions]; void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush]handleLaunching's successBlock"); }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush]handleLaunching's errorBlock"); }; //角标清0 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; //清除所有通知(包含本地通知) // [[UIApplication sharedApplication] cancelAllLocalNotifications]; [XGPush handleLaunching:launchOptions successCallback:successBlock errorCallback:errorBlock]; } #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_ //注册UserNotification成功的回调 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //用户已经允许接收以下类型的推送 //UIUserNotificationType allowedTypes = [notificationSettings types]; } //按钮点击事件回调 - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ if([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]){ NSLog(@"ACCEPT_IDENTIFIER is clicked"); } completionHandler(); } #endif - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { //NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush Demo]register successBlock"); if (_isLaunchedByNotification) { if ([_remoteNotification[@"grabOrder"]integerValue]==31) { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center postNotificationName:@"grab" object:nil userInfo:nil]; } } }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush Demo]register errorBlock"); }; // 设置账号 要调用注册deviceToken方法 [XGPush setAccount:@"abcdefg"]; //注册设备 NSString * deviceTokenStr = [XGPush registerDevice:deviceToken successCallback:successBlock errorCallback:errorBlock]; //打印获取的deviceToken的字符串 NSLog(@"[XGPush Demo] deviceTokenStr is %@",deviceTokenStr); } //如果deviceToken获取不到会进入此事件 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSString *str = [NSString stringWithFormat: @"Error: %@",err]; NSLog(@"[XGPush Demo]%@",str); } //前台和后台都会是触发这个方法 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { _isLaunchedByNotification = YES; //推送反馈(app运行时) [XGPush handleReceiveNotification:userInfo]; //回调版本 //NSString * deviceTokenStr = [XGPush registerDevice:deviceToken]; void (^successBlock)(void) = ^(void){ //成功之后的处理 NSLog(@"[XGPush Demo]handelReceiveNotification successBlock"); }; void (^errorBlock)(void) = ^(void){ //失败之后的处理 NSLog(@"[XGPush Demo]handelReceiveNotification errorBlock"); }; NSLog(@"userInfo - %@",userInfo); void (^completion)(void) = ^(void){ //完成之后的处理 //userinfo里是运行时推送的弹窗信息 int i =0; i = i + [userInfo[@"badge"] intValue]; [UIApplication sharedApplication].applicationIconBadgeNumber=i; if (_isBackGround) { //用这个bool值判断是否是从后台运行时打开 _isBackGround =NO; }else { //不是后台运行时打开后的操作 } NSLog(@"[xg push completion]userInfo is %@",userInfo); }; [XGPush handleReceiveNotification:userInfo successCallback:successBlock errorCallback:errorBlock completion:completion]; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end 其他问题请移步点击打开链接查看是否能够解决~

    最后!!!还有一个坑!!!

    一个账号最多绑定15个设备号,

    就是说,我们测试的话最多测十五次,

    这就是为什么我前一天测试测得好好的,结果第二天不行了的原因!!!

    大家一定要注意!!!

    转载请注明原文地址: https://ju.6miu.com/read-900062.html

    最新回复(0)