1. 集成前的准备
集成之前, 请在http://push.umeng.com/申请开通【友盟+】消息推送服务
请先在【友盟+】消息推送管理后台中创建App,获得AppKey和AppSecret
2.1 导入SDK
所需SDK文件夹:UMessage_Sdk_x.x.x请在你的工程目录结构中,右键选择Add->Existing Files…,选择这个文件夹。或者将这个文件夹拖入XCode工程目录结构中,在弹出的界面中勾选Copy items into destination group's folder(if needed), 并确保Add To Targets勾选相应的target。
2.2 引入库文件
增加UserNotifications.framework到项目中。具体操作如下:点击项目---->TARGET---->Build Phases---->Link Binary with Libraries ---->左侧+号---->搜索UserNotifications---->选中UserNotifications.framework---->点击Add
(如果使用pod直接可以pod 'UMessage_NOIDFA')
2.3 配置(可选)
如果您使用了-all_load,可能需要添加libz的库:
TARGETS-->Build Phases-->Link Binary With Libraries--> + -->libz.dylib2.4 打开推送开关
点击项目---->TARGET---->Capabilities,将这里的Push Notification的开关打开,效果如图所示: 这里写图片描述注意:一定要打开Push Notification,且两个steps都是正确的,否则会报如下错误:Code=3000 "未找到应用程序的“aps-environment”的授权字符串"
3 集成推送
打开*AppDelegate.m3.1 引入头文件
引入UMessage.h,UserNotifications.h3.2 设置代理
设置UNUserNotificationCenterDelegate
3.3 AppDelegate调用下面的方法
下面是代码方便复制:
#pragma mark 友盟推送
-(void)startUmengPush:(NSDictionary *)launchOptions
{
[UMessage startWithAppkey:@"58d0d071e88bad6b68000ec0"launchOptions:launchOptions];
[UMessage registerForRemoteNotifications];
//iOS10必须加下面这段代码。
UNUserNotificationCenter *center = [UNUserNotificationCentercurrentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions types10 =UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10completionHandler:^(BOOL granted,NSError * _Nullable error) {
if (granted) {
//点击允许
//这里可以添加一些自己的逻辑
} else {
//点击不允许
//这里可以添加一些自己的逻辑
}
}];
[UMessage setLogEnabled:YES];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中调用[selfstartUmengPush:launchOptions];
iOS 10以上的系统还需调用如下代码:
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
//应用处于前台时的远程推送接受
//关闭U-Push自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于前台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
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.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于后台时的本地推送接受
}
}
到此集成基本上完毕了。还需要更多功能的需要到官网看文档。证书问题去官网上看文档里面有详细的配置。
(注意iOS9系统在测试模式下:个推可以,群推不行)
至于在iOS9上的问题,那是因为苹果出了iOS10之后,苹果推送的后台有时会提供给你的device_token是生产模式下的device_token,这时苹果的一个bug。如果总提示发送失败或者提示device_token无效或者提示无法验证该设备的有效性,不保证消息能够发送成功等,在保证device_token是最新的device_token的情况下,使用单播的方式,在生产模式下向该device_token发送推送消息。就可以接收到消息了。完美解决。这是苹果的bug,不用管它。如果这样你还没接收到消息,那就是你的问题或者友盟的问题了。