I am using the phonegap-plugin-push plugin to do Push Notifications in an Ionic 2 Chat App.
I've implemented it according to the documentation, and it works, but only on the same device. i.e. it sends a notification to the phone if you supply its device token
.
My issue is for a chat app, I need a PubSub model. So that a user can publish to a topic and another user can subscribe to that topic even if they are on different decices.
Looking at the documentation, it seems to be possible. See android.topics
and ios.topics
.
android.topics array [] Optional. If the array contains one or more strings each string will be used to subscribe to a GcmPubSub topic.
So I try the following:
Javascript Client:
publish
let push = Push.init({
android: {
senderID: "xxxxxxxx",
topics: ['foo']
},
ios: {
alert: "true",
badge: false,
sound: "true",
topics: ['foo']
},
windows: {}
});
push.on('registration', (data) => {
// call the Java Server
let promise: Promise<string> = this.notificationService.push(data.registrationId, 'This is a test message from Ionic Chat');
promise.then((data) => {
});
});
Java Server:
try {
System.out.println("NotificationService: device_token: "+device_token);
logger.info("NotificationService: device_token: "+device_token);
// Prepare JSON containing the GCM message content. What to send and where to send.
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
jData.put("title", "This is title");
jData.put("message", message);
jGcmData.put("to", device_token); // <===== is this the problem???? it is the data.registrationId from the client
// What to send in GCM message.
jGcmData.put("data", jData);
// Create connection to send GCM Message request.
URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "key=" + API_KEY);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
// Send GCM message content.
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
Javascript Client:
Subscribe
let push = Push.init({
android: {
senderID: "xxxxxxxx",
topics: ['foo']
},
ios: {
alert: "true",
badge: false,
sound: "true",
topics: ['foo']
},
windows: {}
});
push.on('notification', (data) => {
});
It works on the same device, but it doesn't work if the publisher
and subscriber
are on different devices.
I think the problem may be that even though another user is subscribed to the topic I publish to, it is still only publishing to the data.registrationId
(Device Token).
See the following line on the Java Server:
jGcmData.put("to", device_token);
Question
Does anyone know how to make it send to all subscribers on that topic?
I think the answer is here:
"to": "/topics/foo-bar",
But how should this be formatted for multiple topics? (that's just one topic foo-bar
)
Need to Change the Java Server to send to the topic instead of the device:
see: https://developers.google.com/cloud-messaging/