{
"aps":{
"alert":{
"body":"您有一条新消息"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
{
"aps":{
"alert":{
"body":"ApnsNickName:xxxx"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
{
"aps":{
"alert":{
"body":"您有一条新消息"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"g":"1421300621769",
"m":"373360335316321408"
}
{
"aps":{
"alert":{
"body":"ApnsName:xxxx"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"g":"1421300621769",
"m":"373360335316321408"
}
APNs 扩展(em_apns_ext):添加后,您收到的 APNs 中将带有您填写的字段,可以帮助您区分 APNs。
环信提供以下几种扩展字段:
扩展字段 | 描述 |
---|---|
em_push_content | 自定义推送显示 |
em_push_category | 向 APNs Payload 中添加 category 字段 |
em_push_sound | 自定义推送提示音 |
em_push_mutable_content | 开启 APNs 通知扩展 |
{
"apns": {
"alert": {
"body": "hello from rest"
},
"badge": 1,
"sound": "default"
},
"e": "自定义推送扩展",
"f": "6001",
"t": "6006",
"m": "373360335316321408"
}
(REST 发消息)
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"ext": {
"em_apns_ext": {
"extern": "自定义推送扩展"
}
},
"from": "6001"
}
(iOS 发消息)
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];
设置后,您收到的 APNs 的 alert 信息将是您设置的信息。
{
"aps":{
"alert":{
"body":"自定义推送显示"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
(REST 发消息)
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"from": "6001",
"ext": {
"em_apns_ext": {
"em_push_content": "自定义推送显示"
}
}
}
如果要兼容Android端,需要在消息的扩展中增加以下字段
"ext":{
...
"em_android_push_ext":{
//指定自定义渠道
"em_push_channel_id":"Channel id",
"em_push_sound":"/raw/appsound"
}
}
(iOS 发消息)
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":@{@"em_push_content":@"自定义推送显示"}}; // 此处的ext和message初始化时传递的ext效果是一样的,此处单独抽出来的目的是表示的更清晰。
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
自定义显示与自定义扩展同时发给对方。
{
"aps":{
"alert":{
"body":"自定义推送显示"
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408",
"e": "自定义推送扩展",
}
(REST 发消息)
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"from": "6001",
"ext": {
"em_apns_ext": {
"em_push_content": "自定义推送显示",
"extern": "自定义推送扩展"
}
}
}
(iOS 发消息)
NSString *willSendText = [EaseConvertToCommonEmoticonsHelper convertToCommonEmoticons:text];
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":@"自定义推送扩展",@"em_push_content":@"自定义推送显示"}}; // 此处的ext和message初始化时传递的ext效果是一样的,此处单独抽出来的目的是表示的更清晰。
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
向 APNs Payload 中添加 category 字段。
(REST 发消息)
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"from": "6001",
"ext": {
"em_apns_ext": {
"em_push_category" : "NEW_MESSAGE_CATEGORY"
}
}
}
设置后,您收到的 APNs 的提示音将是您设置的提示音。
注:
支持格式 Linear PCM MA4 (IMA/ADPCM) µLaw aLaw
存放路径 AppData/Library/Sounds,时长不得超过30秒。 具体信息,可以参考苹果官方文档Generating a Remote Notification
{
"aps":{
"alert":{
"body":"您有一条新消息"
},
"badge":1,
"sound":"custom.caf"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
(REST 发消息)
注:“em_push_sound”为设置自定义 APNs 提示音的扩展字段,value 值为音频文件名,字符串类型。
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"from": "6001",
"ext": {
"em_apns_ext": {
"em_push_sound": "custom.caf"
}
}
(iOS 发消息)
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":@{@"em_push_sound":@"custom.caf"}}; // 设置自定义APNs提示音的扩展字段,value值为音频文件名,字符串类型
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
(UNNotificationServiceExtension)
设置后,该条消息的 APNs 推送将在服务端支持 UNNotificationServiceExtension ,还需要APP在项目中集成 UNNotificationServiceExtension ,才可使用 APNs 通知扩展。iOS 集成方式见 Apple官方文档
{
"aps":{
"alert":{
"body":"您有一条新消息"
},
"badge":1,
"sound":"default",
"mutable-content":1
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
(REST 发消息)
注:“em_push_mutable_content”的 value 值为 bool 类型,true 为开启;false 或不设置,则是普通的 Remote Notification。
{
"target_type": "users",
"target": [
"6006"
],
"msg": {
"type": "txt",
"msg": "hello from rest"
},
"from": "6001",
"ext": {
"em_apns_ext": {
"em_push_mutable_content": true
}
}
}
(iOS 发消息)
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":@{@"em_push_mutable_content":@YES}}; // @YES为开启,@NO或不设置,则是普通的 Remote Notification
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
环信消息的离线推送可支持 APNs 以下几个国际化配置的 key:title-loc-key
,title-loc-args
,loc-key
,loc-args
关于这几个 key 的官方解释:
注:
title-loc-key
与 title-loc-args
组成一对,没有 title-loc-key
, title-loc-args
不生效,如无可变参数,可以只设置 title-loc-key
;loc-key
与 loc-args
组成一对,没有 loc-key
, loc-args
不生效,如无可变参数,可以只设置 loc-key
;Localizable.strings
,如 demo 中配置了 Localizable.strings (Chinese (Simplified))
、Localizable.strings (English)
;Localizable.strings
中配置 title-loc-key
和 loc-key
,并赋值,需要使用 title-loc-args
和 loc-args
中的可变参数的字符串,用 %@
代替;如下:
在 Localizable.strings (Chinese (Simplified))
中配置:
"GAME_PLAY_REQUEST_FORMAT" = "%@ 游戏邀请 %@";
"GAME_PLAY_REQUEST" = "游戏邀请";
在 Localizable.strings (English)
中配置:
"GAME_PLAY_REQUEST_FORMAT" = "%@ GAME_PLAY_REQUEST_FORMAT %@";
"GAME_PLAY_REQUEST" = "GAME_PLAY_REQUEST_CUSTOM";
{
"target_type" : "users",
"target" : ["6006"],
"msg" : {
"type" : "txt",
"msg" : "hello from rest"
},
"ext": {
"em_apns_ext": {
"em_push_content": "自定义推送显示", # 可以同时设置自定义推送显示,没有配置loc-key的app,会显示"自定义推送显示"
"em_push_title_loc_key" : "GAME_PLAY_REQUEST_FORMAT", # 对应title_loc_key
"em_push_title_loc_args" : ["Shelly", "Rick"], # 对应title_loc_args
"em_push_body_loc_key" : "GAME_PLAY_REQUEST_FORMAT", # 对应loc_key
"em_push_body_loc_args" : ["Shelly", "Rick"] # 对应loc_args
}
},
"from" : "6001"
}
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":@{@"em_push_title_loc_key":@"GAME_PLAY_REQUEST_FORMAT", @"em_push_title_loc_args":@[@"Shelly", @"Rick"], @"em_push_body_loc_key":@"GAME_PLAY_REQUEST_FORMAT", @"em_push_body_loc_args":@[@"Shelly", @"Rick"]}};
message.chatType = EMChatTypeChat; // 设置消息类型
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
{
"aps":{
"alert":{
"body":"自定义推送显示",
"title-loc-key":"GAME_PLAY_REQUEST_FORMAT",
"title-loc-args":["Shelly","Rick"],
"loc-key":"GAME_PLAY_REQUEST_FORMAT",
"loc-args":["Shelly","Rick"]
},
"badge":1,
"sound":"default"
},
"f":"6001",
"t":"6006",
"m":"373360335316321408"
}
(em_ignore_notification)
不发APNs。发送时添加后,该消息将不会有 APNs 推送。
(REST 发消息)
{
"target_type":"users",
"target":[
"6006"
],
"msg":{
"type":"txt",
"msg":"hello from rest"
},
"from":"6001",
"ext":{
"em_ignore_notification":true
}
}
(iOS 发消息)
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"test"];
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6006" from:@"6001" to:@"6006" body:body ext:nil];
message.ext = @{@"em_ignore_notification":@YES};
message.chatType = EMChatTypeChat; // 设置消息类型
// 发送消息示例
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
(em_force_notification)
设置后,将强制推送消息,即使客户端设置了免打扰时间,也会得到推送。优先级比 em_ignore_notification 低,即同时设置 em_ignore_notification 后,该属性将失效。
(REST 发消息)
{
"target_type":"users",
"target":[
"6006"
],
"msg":{
"type":"txt",
"msg":"hello from rest"
},
"from":"6001",
"ext":{
"em_force_notification":true
}
}
(iOS 发消息)
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"test"];
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6006" from:@"6001" to:@"6006" body:body ext:nil];
message.ext = @{@"em_force_notification":@YES};
message.chatType = EMChatTypeChat; // 设置消息类型
// 发送消息示例
[EMClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];