iOS 环信推送、对方昵称

环信离线推送并不难, 只是被网上云里雾里的教程搞得晕头转向, 不得不说, 有问题找官方客服更便利

官方技术支持在每个项目展开后的右下角, 很难看到.....

推送的关键是:

[[EMClient sharedClient] bindDeviceToken:deviceToken];

推送前提是初始化:

//环信的appkey
EMOptions *options = [EMOptions optionsWithAppkey:@"xxxxxxxxxxxxxxxxx#yyyyyyy"];
//环信ios证书对应的名称, 不要填错, 填错离线无法推送
options.apnsCertName = @"dev";
[[EMClient sharedClient] initializeSDKWithOptions:options];
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];

推送非必要条件:

官方帮助文档中有这样一段代码:

-(void)initPush{
    UIApplication *application = [UIApplication sharedApplication];
    //iOS10 注册APNs
    if (NSClassFromString(@"UNUserNotificationCenter")) {
        [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError *error) {
            if (granted) {
#if !TARGET_IPHONE_SIMULATOR
                [application registerForRemoteNotifications];
#endif
            }
        }];
        return;
    }

    if([application respondsToSelector:@selector(registerUserNotificationSettings:)]){
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }

#if !TARGET_IPHONE_SIMULATOR
    if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
        [application registerForRemoteNotifications];
    }else{
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeSound |
        UIRemoteNotificationTypeAlert;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
#endif
}

本段代码只是为了能让appdelegate类响应这个方法
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
而如果能响应就不要再调用这个方法了

推送是否显示详情

EMPushOptions *options = [[EMClient sharedClient] pushOptions];
options.displayStyle = EMPushDisplayStyleMessageSummary // 显示消息内容
// options.displayStyle = EMPushDisplayStyleSimpleBanner // 显示“您有一条新消息”
EMError *error = [[EMClient sharedClient] updatePushOptionsToServer]; // 更新配置到服务器,该方法为同步方法,如果需要,请放到单独线程
if(!error) {
   // 成功
}else {
   // 失败
}

对方昵称很好实现

但原理是反过来, 谁登录就设置自己的昵称, 然后发送推送的时候, 这个昵称会顺带一起发送过去, 接收到的人会看到这个昵称, 所以需要实现的是发送方设置它

[[EMClient sharedClient] setApnsNickname:nickname];

推送添加额外字段

EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"test"];
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6006" from:@"6001" to:@"6006" body:body ext:nil];
message.ext = @{@"em_apns_ext":@{@"extern":@"自定义推送扩展"}}; // 此处的ext和message初始化时传递的ext效果是一样的,此处单独抽出来的目的是表示的更清晰。
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];

其中的 message.ext = @{@"em_apns_ext":@{@"extern":@"自定义推送扩展"}};

你可能感兴趣的:(iOS 环信推送、对方昵称)