Android GCM - Send different payload to each user in registration_ids array

时间秒杀一切 提交于 2019-12-12 13:14:04

问题


In a multicast GCM trigger, I want to send different payload to each user. My GCM json corresponding array is as follows:

$fields = array(
'collapse_key' => 'demo',
'registration_ids' => $registration_ids,
'data' => array('myjson' => 'abc'),
);

Consider the registeration_ids array as follows:

$registration_ids => array('id1','id2','id3');

With the above mentioned GCM json corresponding array, all the users get same value of 'myjson'. Is there any way that i can design the json corresponding array such that 'id1' gets 'abc' as the value of 'myjson', 'id2' gets 'def' as the value of 'myjson'... and so on?


回答1:


It's not possible. A multicast message allows you only to send the same message to multiple registration IDs.

  • A 3rd-party application server can either send messages to a single device or to multiple devices. A message sent to multiple devices simultaneously is called a multicast message.
  • To send a single message to multiple devices owned by a single user, you can use a notification_key, as described in User Notifications.
  • You have 2 choices in how you construct requests and responses: plain text or JSON.
  • However, to send multicast messages, you must use JSON. Plain text will not work.

registration_ids A string array with the list of devices (registration IDs) receiving the message. It must contain at least 1 and at most 1000 registration IDs. To send a multicast message, you must use JSON. For sending a single message to a single device, you could use a JSON object with just 1 registration id, or plain text (see below). A request must include a recipient—this can be either a registration ID, an array of registration IDs, or a notification_key.

Note that in a multicast message you are still sending a single message. The only parameter that can have multiple values in the JSON request is the registration_ids.

To send different messages to different devices, you must send one request (either JSON with a single registration ID or plain text) for each device.




回答2:


Make it a map like this:

{
  regn: [{
          regn_id: id_1,
          data: abc
         },
         {
          regn_id: id_2,
          data: defg
         },
       ..]
}

Call it in a JSONArray object.



来源:https://stackoverflow.com/questions/17692353/android-gcm-send-different-payload-to-each-user-in-registration-ids-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!