一、SDK下载地址:http://dev.xiaomi.com/mipush/downpage/
二、登录小米开发者网站http://developer.xiaomi.com。注册App,申请对应的AppID, AppKey, AppSecret。其中AppID和AppKey是客户端的应用标识,在客户端SDK初始化时使用;AppSecret是应用私钥,在使用服务器 SDK向客户端发送消息时使用。
三、创建APNs证书
APNs证书是一种扩展名为p12的文件,它是我们发送消息给APNs的证明。在开发时我们分Development环境与Distribution环境。所以p12会有两个版本:Development与 Distribution。NOTE: 创建证书时请携带密码。
五、搭建XCode运行环境
我们的libMiPushSDK库文件同时包含i386、x86_64、arm64、armv6和armv7的代码,所以libMiPushSDK将同时支持真机与模拟器。但由于Apple推送不支持模拟器,请使用真机测试功能。
1.小米推送服务IOS版支持的最低系统版本iOS5.0。
2.添加libMiPushSDK.a , MiPushSDK.h 到工程。
3.引入库 UserNotifications.framework(iOS10+),libresolv.dylib,libxml2.dylib,libz.dylib,
SystemConfiguration.framework,MobileCoreServices.framework,
CFNetwork.framework,CoreTelephony.framework(如果已经引入,请忽略)。
4.target的Capabilities选项卡打开Push Notifications(如果Xcode版本没有此选项忽略)。
六、配置SDK运行参数
1.打开工程下资源文件Info.plist
2.文件为源代码形式打开,添加以下信息
3.如果开发版本较多,修改连接环境比较繁琐,提供以下方式。!--请注意MiSDKRun写法, 用buildSetting里面的宏替换
其中,MiSDKAppID, MiSDKAppKey为在小米开发者网站http://developer.xiaomi.com,注册App后的AppID,AppKey。MiSDKRun是设定SDK是连接Development环境/Distribution环境 对应参数为debug/online。
!!!注意:因为在测试的时候必须是Development环境下,不然在Distribution环境下是针对线上的不能测试,所以在测试的时候,对应参数一定为debug。我遇到的问题是,这里已经设置了debug,证书为测试证书,但是后台人员还是发送消息失败,提示'发送消息失败'。找到原因:后台开发代码也要分别设置推送消息环境。。。。
七、在项目工程Xcode Appdelegate中配置代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// 同时启用APNs跟应用内长连接
[MiPushSDK registerMiPush:self type:0 connect:YES];
// 点击通知打开app处理逻辑
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(userInfo){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"消息"
message:[NSString stringWithFormat:@"%@", userInfo]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *act = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:act];
[nav presentViewController:alert animated:YES completion:nil];
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
#pragma mark 注册push服务.
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[NSString stringWithFormat:@"注册APNS成功:APNS token: %@", [deviceToken description]];
// 注册APNS成功, 注册deviceToken
[MiPushSDK bindDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
{
[NSString stringWithFormat:@"注册APNS失败:APNS error: %@", err];
// 注册APNS失败.
// 自行处理.
}
#pragma mark Local And Push Notification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[NSString stringWithFormat:@"APNS notify: %@", userInfo];
// 当同时启动APNs与内部长连接时, 把两处收到的消息合并. 通过miPushReceiveNotification返回
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}
// iOS10新加入的回调方法
// 应用在前台收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[NSString stringWithFormat:@"APNS notify: %@", userInfo];
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}
// completionHandler(UNNotificationPresentationOptionAlert);
}
// 点击通知进入应用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[NSString stringWithFormat:@"APNS notify: %@", userInfo];
[MiPushSDK handleReceiveRemoteNotification:userInfo];
}
completionHandler();
}
#pragma mark MiPushSDKDelegate
- (void)miPushRequestSuccWithSelector:(NSString *)selector data:(NSDictionary *)data
{
[NSString stringWithFormat:@"command succ(%@): %@", [self getOperateType:selector], data];
if ([selector isEqualToString:@"registerMiPush:"]) {
}else if ([selector isEqualToString:@"registerApp"]) {
// 获取regId。1、在不同环境下获取的regId是不同的。2、每次重新下载获取的regId也是不同的。
NSLog(@"regid = %@", data[@"regid"]);
}else if ([selector isEqualToString:@"bindDeviceToken:"]) {
[MiPushSDK setAlias:@"dailybuild_test_alias"];
[MiPushSDK subscribe:@"dailybuild_test_topic"];
[MiPushSDK setAccount:@"dailybuild_test_account"];
// 获取regId
NSLog(@"regid = %@", data[@"regid"]);
}else if ([selector isEqualToString:@"unregisterMiPush"]) {
}
}
- (void)miPushRequestErrWithSelector:(NSString *)selector error:(int)error data:(NSDictionary *)data
{
[NSString stringWithFormat:@"command error(%d|%@): %@", error, [self getOperateType:selector], data];
}
- (void)miPushReceiveNotification:(NSDictionary*)data
{
// 1.当启动长连接时, 收到消息会回调此处
// 2.[MiPushSDK handleReceiveRemoteNotification]
// 当使用此方法后会把APNs消息导入到此
[NSString stringWithFormat:@"XMPP notify: %@", data];
}
- (NSString*)getOperateType:(NSString*)selector
{
NSString *ret = nil;
if ([selector hasPrefix:@"registerMiPush:"] ) {
ret = @"客户端注册设备";
}else if ([selector isEqualToString:@"unregisterMiPush"]) {
ret = @"客户端设备注销";
}else if ([selector isEqualToString:@"registerApp"]) {
ret = @"注册App";
}else if ([selector isEqualToString:@"bindDeviceToken:"]) {
ret = @"绑定 PushDeviceToken";
}else if ([selector isEqualToString:@"setAlias:"]) {
ret = @"客户端设置别名";
}else if ([selector isEqualToString:@"unsetAlias:"]) {
ret = @"客户端取消别名";
}else if ([selector isEqualToString:@"subscribe:"]) {
ret = @"客户端设置主题";
}else if ([selector isEqualToString:@"unsubscribe:"]) {
ret = @"客户端取消主题";
}else if ([selector isEqualToString:@"setAccount:"]) {
ret = @"客户端设置账号";
}else if ([selector isEqualToString:@"unsetAccount:"]) {
ret = @"客户端取消账号";
}else if ([selector isEqualToString:@"openAppNotify:"]) {
ret = @"统计客户端";
}else if ([selector isEqualToString:@"getAllAliasAsync"]) {
ret = @"获取Alias设置信息";
}else if ([selector isEqualToString:@"getAllTopicAsync"]) {
ret = @"获取Topic设置信息";
}
return ret;
}
八、可以在小米官网测试,也可以后台推送下次测试(记得选择测试环境)
1.jpeg九、测试成功就可以发布上线了
!!!注意:发布上线时,一定记得后台推送环境改为正式环境。MiSDKRun是设定SDK是连接Distribution环境 对应参数为online。
网友评论