Push Notifications in Ionic 2
<p>I am building a Chat application in Ionic 2, and need to implement Push Notifications.</p>

<p>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 <a href="https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.hivc89xkk" rel="nofollow noreferrer">following tutorial</a> which looks to do exactly what I need.</p>

<p>The tutorial seems good, however, I am confused on a few issues, and any help would be appreciated.</p>

<p>I am developing on a Windows machine, so currently just building for Android.</p>

<p><strong>Question 1</strong></p>

<p>I am confused with the following statement:</p>

<blockquote>
  <p>Build and run on real android and iOS devices to see device token in
  console.</p>
</blockquote>

<p>When I run <code>ionic build android</code>, it does not generate a <strong>device token</strong> in the console. Do you know how I can see what the <strong>device token</strong> is?</p>

<p>Or, is it just generated at run time by <code>data.registrationId</code>, and this is passed to the server?</p>

<pre><code>        push.on('registration', (data) =&gt; {
            console.log("device token -&gt;", data.registrationId);
            //TODO - send device token to server
        });
</code></pre>

<p><strong>Question 2</strong></p>

<p>In the Java,</p>

<pre><code>static String API_KEY = "YOUR_GCM_API_KEY";
static String device_token = "YOUR_DEVICE_TOKEN";
</code></pre>

<p>For <code>static String API_KEY = "YOUR_GCM_API_KEY";</code>, is the <code>YOUR_GCM_API_KEY</code> the <strong>Server Key</strong> or <strong>Sender ID</strong> from <strong>FCM</strong>?</p>

<p>Is the <code>static String device_token = "YOUR_DEVICE_TOKEN";</code> received from the Ionic app when you do this: <code>console.log("device token -&gt;", data.registrationId);</code>? i.e. Is it passed from the client to the server?</p>

<p><a href="https://i.stack.imgur.com/5Nshl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5Nshl.png" alt="enter image description here"></a></p>

<p><strong>Question 3</strong></p>

<p>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.<code>[userId1,userId2]</code>), but your example uses a device token, how do you send it for the unique user pair rather?</p>

<p><strong>Question 4</strong></p>

<p>I get the following at run time in the browser.</p>

<blockquote>
<pre><code>EXCEPTION: Error: Uncaught (in promise): TypeError: push.on is not a function
</code></pre>
</blockquote>

<p>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.</p>

<p><strong>UPDATE</strong></p>

<p>It looks like all questions, apart from <strong>Question 3</strong> 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.</p>