IOS原生接入友盟推送

一、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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值