Push Notification: What does push.init(pushOptions) exactly do?


#1

I am implementing Push Notification in my app and a bit confused on what some of the things mean. Can somebody please point me to correct documentation about Push Notification in Ionic?

For example, I am not sure what the following actually does. I mean, from the simple english point of view, it’s basically initializing, I hope.

let pushOptions: PushOptions = {
     android: { senderID: 'XXXXXXXXXX' },
     ios: { alert: 'true', badge: 'true', sound: 'true' },
     windows: { }
};

let pushObject: PushObject = this.push.init(pushOptions);

pushObject.on('registration').subscribe((data: any) => {.....

From the last statement, it looks like the this.push.init() is registering my app(?) or (device?) with the PNS specified in pushOptions.
It gives a “registrationId” which I can use to identify a device while sending push notification.

I think I figured out somewhat but is there any documentation available?

Thanks.


#2

What is this in your code? I have no idea what push library you are using.


#3

@Sujan12 - that’s the way I saw with Ionic 3. Is it not?

Here is what I have done after going through online material I found about Ionic 3 way of implementing Push Notification functionality.

Following is the code I have:

Import:

import { Push, PushObject, PushOptions } from '@ionic-native/push';

Constructor:

    constructor(public http: Http, public platform: Platform,
                public events: Events, public push: Push,
                public alertCtrl: AlertController) {

    }

And then in my Push Notification initialization/registration function I have the code as in my original post, like below:

    registerForPushNotifications() {
        if (!this.platform.is('cordova')) {
            console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
            return;
        }        

        let pushOptions: PushOptions = {
          android: { senderID: 'XXXXXXXXX' },
          ios: { alert: 'true', badge: 'true', sound: 'true' },
          windows: { }
        };

        let pushObject: PushObject = this.push.init(pushOptions);

        pushObject.on('registration').subscribe((data: any) => {
            console.log('Device token/handle is: ' + data.registrationId);
            .
            .
            .
        });

        pushObject.on('notification').subscribe((data: any) => {
            console.log('Push Received: ' + data.message);
            alert('New Notification' + data.message);
        });

        pushObject.on('error').subscribe(error => {
            console.log("ERROR is: " + error);            
        });
    }

May be you are the right person to provide more info or link on where to find Ionic documentation for doing this. Please let me know if you have anything.

Thanks.


#4

That is the relevant information. You are using Ionic Native. (I fixed your topic’s category)

Ionic Native Push is “only” a convenient wrapper around the Cordova plugin. Here is the documentation on init and its options:

https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushnotificationinitoptions

To be honest this is not much more real “explanation” than you gave yourself, but that is mainly because there isn’t more to tell. You tell the plugin to do it’s job with some parameters, it returns the data you need to work further with it (registrationId to send push via API of your push provider).


#5

@Sujan12 - thanks for the quick reply, appreciated!

Oh okay, that is native.

So, is there a Ionic only (non-native) Push Notification feature? If yes, could you please provide a link to it?

One more question, when should we use Native and when should we use Ionic specific (non-native) Push Notification implementation? And perhaps, why?

Sorry, lot of questions, but I am a bit confused so wanted to have clear understanding.

I understand it’s probably lot to explain, would appreciate if you could point me to right documentation/blog/forum.

Thanks.


#6

Push is a native feature of smartphones, it only exists in a native context. All Ionic can do is interface with it - which it does via the Ionic Native plugin you are using, via the Cordova plugin you are using (directly or via Ionic Native) or with other Cordova plugins that expose this functionality.

You are doing everything right.

(Ionic also offers Ionic Push at https://docs.ionic.io/services/push/ which is also native, but everything in there comes from Ionic itself. It is a bit more limited though)


#7

@Sujan12 - good to know, I am on right track! :slight_smile:

I will go through that link for Ionic Push too.

Thanks for quick reponses, highly appreciated! :+1: