If you want to allow users to send push messages between them in you application you can achieve it using user-scope topics.
Previously to that it is needed to:
Ensure your application has correctly set the GCM Key (in case of
iOS application, you would need to set the APNS certificate and password.
Ensure the users of the application have correctly set the GCM
registrationID, (or the APNS deviceID in case of iOS application),
it is needed for them to receive push notifications.
Create the channel to send the messages
Create a topic in user scope for user A and subscribe it to the topic, do the same for user B.
Execute this for each user:
// The user must be logged in
try{
// Create a user-scope topic
String topicName = "ContactMe";
KiiTopic topic = KiiUser.topic(topicName);
// Save the topic to Kii Cloud
topic.save();
// Subscribe the current user to the topic
KiiUser user = KiiUser.getCurrentUser();
KiiPushSubscription sub = user.pushSubscription();
sub.subscribe(topic);
} catch (IOException ioe) {
// failed.
} catch (ConflictException e) {
// Already subscribed.
} catch (AppException e) {
// failed.
}
Allow the users to communicate
Now we need to grant permission for sending messages to user B in the topic of user A, reciprocally grant the same permission to user A in topic of user B.
Allow user B to send messages to user A:
// User A must be logged in
try {
// Get user B
KiiUser userB = KiUser.findUserByUserName(userBName);
// Instantiate the user-scope topic of user A
String topicName = "ContactMe";
KiiTopic topic = KiiUser.topic(topicName);
// Get the ACL handler of the topic
KiiACL acl = topic.acl();
// Allow user B to send messages to the topic
KiiACLEntry entry = new KiiACLEntry(userB,
TopicAction.SEND_MESSAGE_TO_TOPIC, true);
acl.putACLEntry(entry);
// Save the new ACL entry
acl.save();
} catch (IOException ioe) {
// failed.
} catch (NotFoundException e) {
// User B not found
} catch (AppException e) {
// failed.
}
To allow user A to send messages to user B, code presented above should be executed changing the roles of the two users.
Send messages
Finally send a message to the topic of the other user.
User A sends a message to user B:
// Loggin with user A
try {
// Get user B
KiiUser userB = KiUser.findUserByUserName(userBName);
// Instantiate the user-scope topic of user B
String topicName = "ContactMe";
KiiTopic topic = userB.topicOfThisUser(topicName);
// Build the push message
KiiPushMessage.Data data = new KiiPushMessage.Data();
data.put("msg", "Hi, how do you do?");
KiiPushMessage msg = KiiPushMessage.buildWith(data).build();
// Send the message to user B
topic.sendMessage(msg);
} catch (IOException ioe) {
// failed.
} catch (NotFoundException e) {
// User B not found
} catch (AppException e) {
// failed.
}
If a user would like to be contacted by any user of the application it could be done adding the following ACL entry in its topic:
KiiACLEntry entry = new KiiACLEntry(KiiAnyAuthenticatedUser.create(),
TopicAction.SEND_MESSAGE_TO_TOPIC, true);