環(huán)信離線推送并不難, 只是被網(wǎng)上云里霧里的教程搞得暈頭轉(zhuǎn)向, 不得不說(shuō), 有問(wèn)題找官方客服更便利
官方技術(shù)支持在每個(gè)項(xiàng)目展開(kāi)后的右下角, 很難看到.....
推送的關(guān)鍵是:
[[EMClient sharedClient] bindDeviceToken:deviceToken];
推送前提是初始化:
//環(huán)信的appkey
EMOptions *options = [EMOptions optionsWithAppkey:@"xxxxxxxxxxxxxxxxx#yyyyyyy"];
//環(huán)信ios證書(shū)對(duì)應(yīng)的名稱, 不要填錯(cuò), 填錯(cuò)離線無(wú)法推送
options.apnsCertName = @"dev";
[[EMClient sharedClient] initializeSDKWithOptions:options];
[[EMClient sharedClient] addDelegate:self delegateQueue:nil];
推送非必要條件:
官方幫助文檔中有這樣一段代碼:
-(void)initPush{
UIApplication *application = [UIApplication sharedApplication];
//iOS10 注冊(cè)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類響應(yīng)這個(gè)方法
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken
而如果能響應(yīng)就不要再調(diào)用這個(gè)方法了
推送是否顯示詳情
EMPushOptions *options = [[EMClient sharedClient] pushOptions];
options.displayStyle = EMPushDisplayStyleMessageSummary // 顯示消息內(nèi)容
// options.displayStyle = EMPushDisplayStyleSimpleBanner // 顯示“您有一條新消息”
EMError *error = [[EMClient sharedClient] updatePushOptionsToServer]; // 更新配置到服務(wù)器貌踏,該方法為同步方法十气,如果需要,請(qǐng)放到單獨(dú)線程
if(!error) {
// 成功
}else {
// 失敗
}
對(duì)方昵稱很好實(shí)現(xiàn)
但原理是反過(guò)來(lái), 誰(shuí)登錄就設(shè)置自己的昵稱, 然后發(fā)送推送的時(shí)候, 這個(gè)昵稱會(huì)順帶一起發(fā)送過(guò)去, 接收到的人會(huì)看到這個(gè)昵稱, 所以需要實(shí)現(xiàn)的是發(fā)送方設(shè)置它
[[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":@"自定義推送擴(kuò)展"}}; // 此處的ext和message初始化時(shí)傳遞的ext效果是一樣的瑟枫,此處單獨(dú)抽出來(lái)的目的是表示的更清晰斗搞。
message.chatType = EMChatTypeChat; // 設(shè)置消息類型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
其中的 message.ext = @{@"em_apns_ext":@{@"extern":@"自定義推送擴(kuò)展"}};