import { Component , ViewChild } from '@angular/core' ; import { AlertController , Nav , Platform } from 'ionic-angular' ; import { StatusBar } from '@ionic-native/status-bar' ; import { SplashScreen } from '@ionic-native/splash-screen' ; import { Push , PushObject , PushOptions } from '@ionic-native/push' ; import { TabsPage } from '../pages/tabs/tabs' ; import { DetailsPage } from '../pages/details/details' ; @ Component ( { template : '<ion-nav [root]="rootPage"></ion-nav>' } ) export class IonicPushApp { @ ViewChild ( Nav ) nav : Nav ; rootPage : any ; constructor ( public platform , public statusBar , public splashScreen , public push , public alertCtrl ) { this . rootPage = TabsPage ; platform . ready ( ) . then ( ( ) => { this . statusBar . styleDefault ( ) ; this . splashScreen . hide ( ) ; this . initPushNotification ( ) ; } ) ; } initPushNotification ( ) { if ( ! this . platform . is ( 'cordova' ) ) { console . warn ( 'Push notifications not initialized. Cordova is not available - Run in physical device' ) ; return ; } const options = { android : { senderID : 'YOUR_SENDER_ID' } , ios : { alert : 'true' , badge : false , sound : 'true' } , windows : { } } ; const pushObject = this . push . init ( options ) ; pushObject . on ( 'registration' ) . subscribe ( ( data ) => { console . log ( 'device token -> ' + data . registrationId ) ; } ) ; pushObject . on ( 'notification' ) . subscribe ( ( data ) => { console . log ( 'message -> ' + data . message ) ; if ( data . additionalData . foreground ) { let confirmAlert = this . alertCtrl . create ( { title : 'New Notification' , message : data . message , buttons : [ { text : 'Ignore' , role : 'cancel' } , { text : 'View' , handler : ( ) => { this . nav . push ( DetailsPage , { message : data . message } ) ; } } ] } ) ; confirmAlert . present ( ) ; } else { this . nav . push ( DetailsPage , { message : data . message } ) ; console . log ( 'Push notification clicked' ) ; } } ) ; pushObject . on ( 'error' ) . subscribe ( error => console . error ( 'Error with Push plugin' + error ) ) ; } }