Created
December 4, 2018 11:23
-
-
Save lfreneda/2a5d2d9227907ad8a5e5ae75ea38ddbe to your computer and use it in GitHub Desktop.
Push Notification Service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { | |
Platform, | |
AlertController | |
} from 'ionic-angular'; | |
import { | |
Push, | |
PushObject | |
} from '@ionic-native/push'; | |
@Injectable() | |
export class PushNotificationService { | |
public registrationToken: string; | |
constructor(private platform: Platform, | |
private push: Push, | |
public alertCtrl: AlertController) { | |
} | |
initPushNotification() { | |
if (!this.platform.is('cordova')) { | |
console.warn('Push notifications not initialized. Cordova is not available - Run in physical device'); | |
return; | |
} | |
const pushObject: PushObject = this.push.init({ | |
android: { | |
senderID: 'senderId' | |
}, | |
ios: { | |
alert: "true", | |
badge: "true", | |
sound: "true", | |
clearBadge: true | |
} | |
}); | |
pushObject.on('registration').subscribe((data) => { | |
console.log('registration device with token', data.registrationId) | |
this.registrationToken = data.registrationId; | |
}); | |
pushObject.on('notification').subscribe((data) => { | |
console.log('notification data', data) | |
if (data.additionalData.foreground) { | |
// if application open, show popup | |
console.log('App is on foreground state'); | |
} else { | |
//if user NOT using app and push notification comes | |
//TODO: Your logic on click of push notification directly | |
// this.nav.push(DetailsPage, { message: data.message }); | |
console.log('Push notification clicked'); | |
} | |
}); | |
pushObject.on('error').subscribe((err) => { | |
console.error('Error with Push plugin' + err) | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment