2

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?

enter image description here

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.

Richard
  • 8,193
  • 28
  • 107
  • 228
  • I developed a post for Ionic Framework 2 push notification from scratch using http://ampersandacademy.com/tutorials/ionic-framework-version-2/push-notification-using-google-firebase – Bharathiraja Feb 24 '17 at 13:08
  • Hey @Richard did you solve this problem, I got the same error in my app. – Ricardo Cunha Mar 28 '17 at 13:46

2 Answers2

0

I haven't tried ionic before, but I'm gonna go on ahead and try to give my ideas with regards to your questions where I think some will be at the very least helpful:

  1. Pretty sure that simply means to run the app on an actual device, not an emulator or a browser. Given the name, I think the reason why they're requiring to run an actual device is because maybe it is an ID that is retrieved from the device itself.

  2. It's the Server Key. NOTE: You should ALWAYS keep the Server Key and Sender ID a secret.

  3. For GCM/FCM one thing that is commonly used is a registration_token, which I think in this case, is the counterpart for the device token.

  4. Not sure with this one. I did see this similar post, though it's still unresolved. Do look around the community, there might be another post similar to it.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Thanks AL. I will keep my keys a secret. That screen print is from the above mentioned tutorial. – Richard Sep 21 '16 at 08:48
0

Question 1

It is a devise token passed from client to server: data.registrationId.

Question 2

YOUR_GCM_API_KEY is the Server Key generated by the FCM. YOUR_DEVICE_TOKEN is passed from the client: data.registrationId.

Question 3

Not sure yet, am investigating further?

Question 4

The error can be ignored, it does not occur on a devise.

Richard
  • 8,193
  • 28
  • 107
  • 228