I am building a Chat application in Ionic 2, and need to implement Push Notifications.
Due to the fact that Google have recently moved their GCM service to rather use FCM, there is not much documentation how to implement an Ionic app with it. I have found the following tutorial which looks to do exactly what I need.
The tutorial seems good, however, I am confused on a few issues, and any help would be appreciated.
I am developing on a Windows machine, so currently just building for Android.
Question 1
I am confused with the following statement:
Build and run on real android and iOS devices to see device token in console.
When I run ionic build android
, it does not generate a device token in the console. Do you know how I can see what the device token is?
Or, is it just generated at run time by data.registrationId
, and this is passed to the server?
push.on('registration', (data) => {
console.log("device token ->", data.registrationId);
//TODO - send device token to server
});
Question 2
In the Java,
static String API_KEY = "YOUR_GCM_API_KEY";
static String device_token = "YOUR_DEVICE_TOKEN";
For static String API_KEY = "YOUR_GCM_API_KEY";
, is the YOUR_GCM_API_KEY
the Server Key or Sender ID from FCM?
Is the static String device_token = "YOUR_DEVICE_TOKEN";
received from the Ionic app when you do this: console.log("device token ->", data.registrationId);
? i.e. Is it passed from the client to the server?
Question 3
If we are sending separate messages to individual devices, e.g. for a Chat App, how do we differentiate between devices? Each chat message has an id associating it with a unique pair of users (e.g.[userId1,userId2]
), but your example uses a device token, how do you send it for the unique user pair rather?
Question 4
I get the following at run time in the browser.
EXCEPTION: Error: Uncaught (in promise): TypeError: push.on is not a function
Can I ignore this? I suspect it is only happening in a browser and I won't get this error when it runs on a real devise.
UPDATE
It looks like all questions, apart from Question 3 have been answered below. I am guessing it can be done with some sort of pub/sub model on a key that's unique to the chat.