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.
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?
@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.
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).
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 Ionic Docs - Ionic Documentation which is also native, but everything in there comes from Ionic itself. It is a bit more limited though)