在接入友盟推送的时候我主要把它分为两步:
第一步:sdk的接入,指在官网下载最新的sdk接入到项目中去,这个比较简单,按照步奏来就可以了。下面我按照我的接入说下:
我下载的3.1.0的sdk
1:解压下载后的sdk,可以看见common/push/thirdparties3个文件夹,把3个文件夹里的.framework放入到项目里并且加入到link FrameWork and Libraries中(点击项目---->TARGET---->Build Phases---->Link Binary with Libraries ---->左侧+号),因为是接入推送所以还需要加入搜索UserNotifications---->选中UserNotifications.framework---->点击Add;
2:打开推送开关
点击项目---->TARGET---->Capabilities,将这里的Push Notification的开关打开,效果如图所示
3:background modes中
打开Remote notifications background mode
4:代码的接入在AppController.mm
4.1:加入头文件:
#import <UMCommon/UMCommon.h> // 公共组件是所有友盟产品的基础组件,必选
#import <UMPush/UMessage.h> // Push组件
#import <UserNotifications/UserNotifications.h> // Push组件必须的系统库
#import <UMErrorCatch/UMErrorCatch.h>
4.2加入UNUserNotificationCenterDelegate
@interface AppController ()<UNUserNotificationCenterDelegate>
@end
@implementation AppController
4.3:友盟的日志
// open um log
[UMConfigure setLogEnabled:true];
4.4友盟推送的初始
[UMConfigure initWithAppkey:[NSString stringWithUTF8String:"596eeedc5312ddacce000879"] channel: [NSString stringWithUTF8String:""]];
[UMErrorCatch initErrorCatch];
// Push组件基本功能配置
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
//type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标等
entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionAlert|UMessageAuthorizationOptionSound;
//[UNUserNotificationCenter currentNotificationCenter].delegate = self;
[UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 用户选择了接收Push消息
CCLOG("===granted=YES===");
}else{
// 用户拒绝接收Push消息
CCLOG("===granted==NO==");
}
}];
//获取测试的token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
CCLOG("===didRegisterForRemoteNotificationsWithDeviceToken===");
NSLog(@"%@",[[[[deviceToken description ] stringByReplacingOccurrencesOfString: @"<" withString:@"" ]
stringByReplacingOccurrencesOfString: @">" withString:@""]
stringByReplacingOccurrencesOfString: @"" withString:@""
]);
//NSLog(@"locError:%s};", NSUserDefaults.kUMessageUserDefaultKeyForParams);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSDictionary *)userInfo
{
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
CCLOG("===didReceiveRemoteNotification===");
// 注意:当应用处在前台的时候,是不会弹出通知的,这个时候就需要自己进行处理弹出一个通知的UI
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] objectForKey:@"title"] message:[[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] objectForKey:@"body"]
delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
return;
}
//如果是在后台挂起,用户点击进入是UIApplicationStateInactive这个状态
else if (application.applicationState == UIApplicationStateInactive){
//......
}
// 这个是友盟自带的前台弹出框
[UMessage setAutoAlert:NO];
if([[[UIDevice currentDevice] systemVersion]intValue] < 10){
[UMessage didReceiveRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
}
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
CCLOG("===willPresentNotification1===");
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
//关闭U-Push自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
CCLOG("===willPresentNotification2===");
}else{
//应用处于前台时的本地推送接受
CCLOG("===willPresentNotification3===");
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法
-(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]]) {
#ifdef UM_Swift
[UMessageSwiftInterface didReceiveRemoteNotificationWithUserInfo:userInfo];
#else
//应用处于后台时的远程推送接受
//必须加这句代码
CCLOG("===didReceiveNotificationResponse===");
[UMessage didReceiveRemoteNotification:userInfo];
#endif
}else{
//应用处于后台时的本地推送接受
}
}
第二步:友盟的后台推送
1:注册IOS应用
需要注意的就是开发证书和生产证书的上传,这个按照http://dev.umeng.com/sdk_integate/ios-integrate-guide/push证书配置指南 这个来就可以了;
开发证书上传后可以进行测试消息的推送,生产证书上传后可以进行正式的推送(正式推送获取token可能需要经过24小时)所以开发和生产证书请都上传好再进行测试;
在这个链接下的
【7. 创建并配置推送证书(开发环境&生产环境)可以查看证书生成方法」】
以Ad Hoc方式打包应用可以进行正式消息的推送
遇到的问题:
1:证书无效,请重新按照步奏导入证书;
2:证书不存在,证书无效或者证书忘记上传了;(都要注意证书选择的appid要和本事项目一致)
3:token无效,证书的问题。也可在前端获取token进行测试
4:正式环境下的token如果你确定接入和证书都没问题,请24小时后在进行测试,这个按友盟的说话,token缓存需要这个时间进行更新(正式环境测试,需要出Ad Hoc的包才可以,否则就会出现发送失败全部token失败的错误)