商业转载请联系作者获得授权,非商业转载请注明出处。
作者:唐大锤
链接:http://www.zhihu.com/question/24857498/answer/29220818
来源:知乎
总体腾讯信鸽推送开发流程
登陆QQ—>注册程序名—>选择应用—>上传证书—>集成开发—>集成开发—>测试应用—>发布应用
第一步,客户端集成
1. 登陆以及使用
信鸽地址: 腾讯信鸽 | 免费专业移动推送,使用QQ登陆,登陆后,填写应用的名称以及资料,填写完成后,提交
<img src="https://pic1.zhimg.com/9d6599ac635cb61ffee24b664b21412c_b.jpg" data-rawwidth="795" data-rawheight="264" class="origin_image zh-lightbox-thumb" width="795" data-original="https://pic1.zhimg.com/9d6599ac635cb61ffee24b664b21412c_r.jpg">2. 申请IOS推送证书,并生成pem文件(熟悉推送证书创建的可跳过这步)另外,官方也提供了详细的教程 :IOS 证书设置指南
(1)登陆 iOS Dev Center 选择进入iOS Provisioning Portal;
(2)在 Identifiers中,点击App IDs进入App ID列表,创建 App ID,为 App 开启 Push Notification 功能;
(3)指定具体的 Bundle ID,创建完成;
(4)在Certificates,中新建一个证书,选择Apple Push Notification service SSL;
(5)点击 Continue 后选择证书对应的App ID(即刚才创建的App ID),继续会需要上传一个本地CSR文件,然后就会生成一个 APNs Push Certificate;
(6)下载并双击打开证书,证书打开时会启动“钥匙串访问”工具。在“钥匙串访问”中你的证书会显示在“我的证书”中,注意选择“My Certificates”和"login";
(7)在“钥匙串访问”中,选择刚刚加进来的证书,选择右键菜单中的“导出“...””。将文件保存为MyApnsCert.p12。保存p12文件时,可以为其设置密码,也可以让密码为空。
(8)把上一步生成的p12证书文件放到桌面,转换成pem证书文件。在终端里运行一下命令转换:
cd desktop
openssl pkcs12 –in MyApnsCert.p12 –outMyApnsCert.pem –nodes
输入提示密码,就是p12的导出密码,桌面就会生成一个pem证书文件
3. 配置新应用
点击应用配置对创建的应用进行配置
<img src="https://pic2.zhimg.com/cbd6afd7b39c44d1f2688ce70981c099_b.jpg" data-rawwidth="757" data-rawheight="123" class="origin_image zh-lightbox-thumb" width="757" data-original="https://pic2.zhimg.com/cbd6afd7b39c44d1f2688ce70981c099_r.jpg">将配置的证书进行上传,每个应用会对应唯一的ACCESS ID ACCESS KEY CECRETKEY
<img src="https://i-blog.csdnimg.cn/blog_migrate/461849fc5979e3ae5d9cbdb29094c435.jpeg" data-rawwidth="3432" data-rawheight="896" class="origin_image zh-lightbox-thumb" width="3432" data-original="https://pic3.zhimg.com/08795d5ea73e6b29899a31160db8cc66_r.jpg">
1. 下载iOS SDK
腾讯信鸽 | 免费专业移动推送 选择iOS SDK 下载后,会得到一个压缩包,我们需要的SDK以及DEMO,都在这个包文件夹里面
2. 集成开发
1、 下载信鸽Pro SDK压缩包到本地并解压;
2、 创建或打开Xcode iOS工程;
3、 将SDK目录下的所有文件添加到Xcode工程;
4、 添加对以下libraries的引用。
核心实现文件为XGPushPro.h和XGPushPro.m。这两个文件整合了XG和MTA来增强信鸽。接口调用的地方请参考AppDelegate.m文件。
API接口
类名 说明
XGPushPro 信鸽Pro版本的Push服务,推送效果
推送被打开效果统计
如果需要统计由信鸽推送的点击或者打开,需要开发者在didReceiveRemoteNotification(如果没有则按照下面的示例手动添加)中调用handleReceiveNotification, 然后在didFinishLaunchingWithOptions中调用handleLaunching。
函数原型:
+(void)handleReceiveNotificationPro:(NSDictionary*)userInfo;//app在前台或者后台运行时
+(void)handleLaunchingPro:(NSDictionary*)launchOptions;//app不在运行时,点击推送激活时
参数说明:
事件本身默认的参数名
返回值:
无
示例:
-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
[XGPushPro handleReceiveNotificationPro:userInfo];
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[XGPushProhandleLaunchingPro: launchOptions];
}
三测试DEMO
APPDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
APPDelegate.m
#import "AppDelegate.h"
#import "XGPush.h"
#import "XGPushPro.h"
#import "MTA.h"
#import "MTAConfig.h"
@implementationAppDelegate
- (void)registerNofitication {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[self registerNofitication];
[[MTAConfig getInstance] setDebugEnable:TRUE]; //是否查看MTA调试信息
[MTA startWithAppkey:@"IG4BJ2YGZ14F"];
//初始化app
[XGPush startApp:2200022728 appKey:@"IMJ34Y25JN4I"];
//推送反馈(app不在前台运行时,点击推送激活时)
[XGPushPro handleLaunchingPro:launchOptions];
return YES;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
//notification是发送推送时传入的字典信息
[XGPush localNotificationAtFrontEnd:notification userInfoKey:@"clockID" userInfoValue:@"myid"];
//删除推送列表中的这一条
[XGPush delLocalNotification:notification];
//[XGPushdelLocationNotification:@"clockID" userInfoValue:@"myid"];
//清空推送列表
//[XGPush clearLocalNotifications];
}
- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
#ifdef DEBUG
//NSLog(@"development mode");
#else
//NSLog(@"distribution mode");
#endif
//注册设备
//[XGPush setAccount:@"123456"];
NSString * deviceTokenStr = [XGPush registerDevice: deviceToken];
//打印获取的deviceToken的字符串
NSLog(@"deviceTokenStr is%@",deviceTokenStr);
}
- (void)application:(UIApplication *)appdidFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
NSString *str = [NSString stringWithFormat: @"Error: %@",err];
NSLog(@"%@",str);
}
- (void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfo
{
//推送反馈(app在前台运行时) 如果是纯净版信鸽
//[XGPushhandleReceiveNotification:userInfo];
//如果是信鸽Pro,上报信息给MTA
[XGPushPro handleReceiveNotificationPro:userInfo];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about tomove from active to inactive state. This can occur for certain types oftemporary interruptions (such as an incoming phone call or SMS message) or whenthe user quits the application and it begins the transition to the backgroundstate.
// Use this method to pause ongoing tasks,disable timers, and throttle down OpenGL ES frame rates. Games should use thismethod to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release sharedresources, save user data, invalidate timers, and store enough applicationstate information to restore your application to its current state in case itis terminated later.
// If your application supports backgroundexecution, this method is called instead of applicationWillTerminate: when theuser quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition fromthe background to the inactive state; here you can undo many of the changesmade on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (ornot yet started) while the application was inactive. If the application waspreviously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about toterminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end