This guide shows you how to create an application (android, iOS or windows) that supports push notifications from Puship through the Ionic Framework platform on the Cordova CLI.
if it is your first app with ionic framework I suggest you follow this guide as to install the necessary tools and do some practice with a first basic application.
ionic start PushipExample --cordova --Il primo argomento è il nome della tua applicazione
Select the javascript framework you prefer to use, in this tutorial we will use Angular.
Select the template from which you want to start to create your project, in this tutorial we will use the Blank template.
Ionic allows you to choose whether to use the CLI Capacitor or Cordova. Since in this guide we will use Cordova select NO to the Capacitor integration request.
-- geolocation plugin ionic cordova plugin add cordova-plugin-geolocation npm install @ionic-native/geolocation -- device plugin ionic cordova plugin add cordova-plugin-device npm install @ionic-native/device -- push plugin ionic cordova plugin add phonegap-plugin-push npm install @ionic-native/push --puship plugin ionic cordova plugin add puship-plugin
import { Push } from '@ionic-native/push/ngx'; providers: [ StatusBar, SplashScreen, Push, { provide: RouteReuseStrategy, useClass: IonicRouteStrategy } ],
import { Component } from '@angular/core'; import { Platform } from '@ionic/angular'; import { SplashScreen } from '@ionic-native/splash-screen/ngx'; import { StatusBar } from '@ionic-native/status-bar/ngx'; import { Push, PushObject, PushOptions } from '@ionic-native/push/ngx'; declare var Puship; @Component({ selector: 'app-root', templateUrl: 'app.component.html', styleUrls: ['app.component.scss'] }) export class AppComponent { constructor( private platform: Platform, private splashScreen: SplashScreen, private push: Push, private statusBar: StatusBar ) { this.initializeApp(); } initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); this.splashScreen.hide(); this.push.hasPermission() .then((res: any) => { if (res.isEnabled) { console.log('We have permission to send push notifications'); } else { console.log('We do not have permission to send push notifications'); } }); //REMOVE THIS SECTION ON IOS // Create a channel (Android O and above). You'll need to provide the id, description and importance properties. this.push.createChannel({ id: "testchannel1", description: "My first test channel", // The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest. importance: 3, //badge is used to if badge appears on the app icon see https://developer.android.com/reference/android/app/NotificationChannel.html#setShowBadge(boolean). //false = no badge on app icon. //true = badge on app icon badge: false }).then(() => console.log('Channel created')); // Delete a channel (Android O and above) this.push.deleteChannel('testchannel1').then(() => console.log('Channel deleted')); // Return a list of currently configured channels this.push.listChannels().then((channels) => console.log('List of channels', channels)) //REMOVE THIS SECTION ON IOS // to initialize push notifications const options: PushOptions = { android: {}, ios: { alert: 'true', badge: true, sound: 'false' }, windows: {}, browser: { pushServiceURL: 'http://push.api.phonegap.com/v1/push' } } const pushObject: PushObject = this.push.init(options); pushObject.on('notification').subscribe((notification: any) => { alert('Received a notification:' + JSON.stringify(notification)); } ); pushObject.on('registration').subscribe((registration: any) => { console.log('Device registered: ', registration); //Puship.EnableLog(true); Puship.Register( registration.registrationId, 'YOUR-PUSHIP-APP-CODE', { successCallback: function (pushipresult) { alert("device registered with DeviceId:" + pushipresult.DeviceId()); }, failCallback: function (pushipresult) { alert("error during registration: " + JSON.stringify(pushipresult)); } } ); }); pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error)); }); } }
ionic build
ionic cordova prepare android //The platform parameter can be android or ios
When the application is started you should see the message “device registered with…”. You can now connect to the Puship Manager and send push notifications to your devices!
Please note:
Below are videos that explain step by step how to create an application and activate push notifications with Puship for Android and iOS:
Below you will find the code used in the videos:
To know in detail how to add tags, manage geolocation and everything related to the other features of the puship api visit the common documentation for all platforms: