Sending a push message to a group-scope or user-scope topic
Sending a message
Any group member can send a push message to a group-scope topic.
Let's explore how to do this in the following sample code:
For a user-scope topic, only the user who owns the topic can send a push message to it.
The following is the sample code.
For both examples, here is a brief description of what is happening in the sample code:
Creates a push message by calling the buildWith method passing the key-value pairs to be included in the message.
Sends the message by calling the sendMessage method.
The refresh() method is executed in the sample code for the group scope. You can omit this method if you use the group just for sending a push message.
Setting fields specific to each push notification network
Sometimes you want to include field values specific to each push notification network in your push messages.
The following sample code shows how to do this.
For adding FCM-specific fields, create a GCMMessage instance by calling the build method.
For adding APNs-specific fields, create an APNSMessage instance by calling the build method.
For adding MQTT-specific fields, create an MqttMessage instance by calling the build method.
When constructing a KiiPushMessage instance, include the GCMMessage, APNSMessage, and/or MqttMessage instances.
If you are going to use the silent notification feature, use the withContentAvailable() method to enable it as shown in the previous sample code. If you want to send notifications with rich content such as images to the user's iOS devices, refer to the lines that are commented out in the sample code to set the related fields.
By default, the push message will be sent to all the push notification networks. You can control message delivery by setting a flag upon construction of a KiiPushMessage instance like in the following example:
// Deliver a push message to Android devices using FCM only.KiiPushMessagemessage=KiiPushMessage.buildWith(data).withGCMMessage(gcm).enableAPNS(false).enableMqtt(false).build();// Deliver a push message to iOS devices only.KiiPushMessagemessage=KiiPushMessage.buildWith(data).withAPNSMessage(apns).enableGCM(false).enableMqtt(false).build();// Deliver a push message using MQTT only.KiiPushMessagemessage=KiiPushMessage.buildWith(data).withMqttMessage(mqtt).enableAPNS(false).enableGCM(false).build();
Saving the APNs payload
The FCM service allows you to use the payload up to 4 KB (4096 bytes) while the APNs payload is limited to 2 KB (2048 bytes).
Kii Cloud adds some predefined keys in the push notification message by default. For example, let us assume that we are going to send an APNs message with its alert body property "short msg only!".
Here is an actual APNs message that will be sent. You will notice that some default keys are included in the message.
sa: The AppID of the application that send the push message.
st: The scope of the topic to which the push message was sent.
"APP" if the topic has the application scope.
"APP_AND_GROUP" if the topic has the group scope.
"APP_AND_USER" if the topic has the user scope.
sg: The ID of the group to which the topic belongs (apply only if the topic has the group scope).
su: The user ID of the owner who owns the topic (apply only if the topic has the user scope).
to: The topic ID of the topic to which the push message was sent.
w: The time when the push message was sent (UNIX time in milliseconds, UTC)
When you want to send a long message, you can suppress these default keys so as to save the APNs payload. See the following sample code to see how you can suppress them.
The above code will send the following push message.