IOS原生接入友盟推送

Coding_Physical發表於2020-12-11

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

 

相關文章