Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap push notifications implementation

Tags:

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?

like image 290
Japa Avatar asked Aug 01 '16 10:08

Japa


2 Answers

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.

like image 156
Rahul Upadhyay Avatar answered Sep 28 '22 04:09

Rahul Upadhyay


PHONEGAP PUSH NOTIFICATION (version 1.3.0)

Follow these steps

  1. You will need to ensure that you have installed the following items through the Android SDK Manager:

    • Android Support Library version 23 or greater Local Maven repository for Support Libraries (formerly Android Support Repository) version 20 or greater
    • Google Play Services version 27 or greater
    • Google Repository version 22 or greater
  2. Install using CLI

    cordova plugin add phonegap-plugin-push --variable SENDER_ID="XXXXXXX"

    Where the XXXXXXX in SENDER_ID="XXXXXXX" maps to the project number in the Google Developer Console. To find the project number login to the Google Developer Console, select your project and click the menu item in the screen shot below to display your project number.

  3. Add code in javascript file

    var push = PushNotification.init({
        android: {
            senderID: "XXXXXXX"
        },
        browser: {
            pushServiceURL: 'http://push.api.phonegap.com/v1/push'
        },
        ios: {
            alert: "true",
            badge: "true",
            sound: "true"
        },
        windows: {}
    });
    
    push.on('registration', function(data) {
        console.log("data.registrationId :"+data.registrationId);
    });
    
    push.on('notification', function(data) {
        // data.message,
        // data.title,
        // data.count,
        // data.sound,
        // data.image,
        // data.additionalData
    });
    
    push.on('error', function(e) {
        // e.message
        //alert("e.message:"+ e.message)
    });
    

Supported Platforms

1. Cordova CLI (3.6.3 or newer)
2. Android (cordova-android 4.0.0 or higher)
3. Browser
4. iOS (cordova-ios 4.1.0 or higher)
5. Windows Universal (not Windows Phone 8)

More...

like image 31
Ved Avatar answered Sep 28 '22 02:09

Ved