Search code examples
cordovaphonegap-pluginsphonegap-buildphonegap-pushpluginphonegap-cli

Phonegap push notifications implementation


I know this type of question has been made before but my problem is, there is so many different answers and let´s just say that the docs are not that good, the thing is, i want to implement push notifications on my app but i´m stuck on step 3 and 4 of this image.

I have followed this tutorial, but when i want to send the private token to my server, what do i have to do to distinguish ios from android?. If you look at this tutorial you will see that there is actually 2 methods to distinguish APNS and GCM(and it´s an old tutorial!), but if you go to phonegap docs or this tutorial that i have been following, the methods are not those.

Does anyone knows a up to date tutorial that i can follow?


Solution

  • To implement push notification you can follow this link (which you have already followed and this is for updated plugin, other tutorial which you have mentioned has explained old deprecated plugin).

    To distinguish ios from android you can send Device Token and Device Platform to your server. In following callback you will receive Device Token for your device which may belongs to any platform (iOS or Android), store this token somewhere for sending it to server:

    push.on('registration', function(data) {
        var deviceToken =  data.registrationId
    });
    

    Now there are two ways to get Device Platform, you can use any of them:

    1. Using device plugin.

      First you need to install this plugin (to install please refer the above link). After installing this plugin you can get the device platform as follows :

      var devicePlatform = device.platform;
      
    2. Using following method :

      function getDevicePlatform() {
          var userAgent = navigator.userAgent || navigator.vendor || window.opera;
          if (/windows phone/i.test(userAgent)) {
              return "Windows";
          }
          if (/android/i.test(userAgent)) {
             return "Android";
          }
          if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
             return "iOS";
         }
         return "unknown";
      }
      

    call this method where ever you need the device platform.

    Now you have both Device Token and Device Platform, send this to your server.

    In server check the device platform first and according to platform you can perform other steps.