一、cocapad 导入友盟三方库
pod 'UMCCommon'
pod 'UMCSecurityPlugins'
pod 'UMCPush'
二、AppDelegate.m配置
- (void)configurationUM:(NSDictionary *)launchOptions{
[UMConfigure setLogEnabled:YES];
[UMConfigure initWithAppkey:@"" channel:@"appstore"];
[UMessage setBadgeClear:NO];
// Push组件基本功能配置
UMessageRegisterEntity * entity = [[UMessageRegisterEntity alloc] init];
//type是对推送的几个参数的选择,可以选择一个或者多个。默认是三个全部打开,即:声音,弹窗,角标
entity.types = UMessageAuthorizationOptionBadge|UMessageAuthorizationOptionSound|UMessageAuthorizationOptionAlert;
[UNUserNotificationCenter currentNotificationCenter].delegate=self;
[UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
}else{
}
}];
NSString*Alias
//绑定别名
[UMessage addAlias: Alias type:@"qiangdan" response:^(id _Nonnull responseObject, NSError * _Nonnull error) {
NSString* str = [NSString stringWithFormat:@"%@",responseObject];
}];
}
//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]]) {
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
NSLog(@"%s--%@",__func__,userInfo);
}else{
//应用处于前台时的本地推送接受
}
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
NSLog(@"%s--%@",__func__,userInfo);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"deviceToken %@",deviceToken);
if (!deviceToken || [deviceToken length] == 0) {
return;
}
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[deviceToken length]];
[deviceToken enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
unsigned char *dataBytes = (unsigned char*)bytes;
for (NSInteger i = 0; i < byteRange.length; i++) {
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
if ([hexStr length] == 2) {
[string appendString:hexStr];
} else {
[string appendFormat:@"0%@", hexStr];
}
}
}];
NSLog(@"deviceToken string %@",string);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"error %@",error.localizedDescription);
}
三、与后端进行联调推送
3.1在友盟后台进行真机调试,在后台创建一个推送消息模块
下一步:
最后一步:
就可以在真机调试获得推送消息;
3.2与后台小哥哥调试推送
这就需要在友盟后台配置IP白名单也就是推送服务器的IP
app需要打包成AdHoc模式安装才能进行调试
四、总结
多看文档 与后台小哥哥一起合作完成该功能
参考资料:
https://developer.umeng.com/docs/119267/detail/119510