目录

APNs 内容解析


单聊

不显示详情

{
	"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 中添加扩展字段

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 发送

REST 发消息

{
    "target_type": "users", 
    "target": [
        "6006"
    ], 
    "msg": {
        "type": "txt", 
        "msg": "hello from rest"
    }, 
    "ext": {
        "em_apns_ext": {
            "extern": "自定义推送扩展"
        }
    }, 
    "from": "6001"
}

iOS 发送

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 发送

(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 发送

(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 发送

REST 发消息

{
    "target_type": "users", 
    "target": [
        "6006"
    ], 
    "msg": {
        "type": "txt", 
        "msg": "hello from rest"
    }, 
    "from": "6001", 
    "ext": {
        "em_apns_ext": {
            "em_push_content": "自定义推送显示",
            "extern": "自定义推送扩展"
        }
    }
}

iOS 发送

(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];

添加category字段

向 APNs Payload 中添加 category 字段。

苹果描述

REST 发送

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 发送

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 发送

(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];

开启 APNs 通知扩展

(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发送

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发送

(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推送国际化配置

环信消息的离线推送可支持 APNs 以下几个国际化配置的 key:title-loc-keytitle-loc-argsloc-keyloc-args

关于这几个 key 的官方解释:

苹果官方开发文档

注:

iOS 客户端配置

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";

发消息时设置消息扩展

REST 发送

{
    "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"
}

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_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 发送

REST 发消息

{
	"target_type":"users",
	"target":[
		"6006"
	],
	"msg":{
		"type":"txt",
		"msg":"hello from rest"
	},
	"from":"6001",
	"ext":{
		"em_ignore_notification":true
	}
}

iOS 发送

(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];

设置强制推送型 APNs

(em_force_notification)

设置后,将强制推送消息,即使客户端设置了免打扰时间,也会得到推送。优先级比 em_ignore_notification 低,即同时设置 em_ignore_notification 后,该属性将失效。

REST 发送

REST 发消息

{
	"target_type":"users",
	"target":[
		"6006"
	],
	"msg":{
		"type":"txt",
		"msg":"hello from rest"
	},
	"from":"6001",
	"ext":{
		"em_force_notification":true
	}
}

iOS 发送

(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];

上一页:APNs 离线推送

下一页:EaseIM 介绍