Last active
May 17, 2018 11:24
-
-
Save senning/a10140d6eb42d1b978dd18020cb6933e to your computer and use it in GitHub Desktop.
Ionic with Cordova-Plugin-Firebase
This file contains hidden or 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 { Nav, Platform, ToastController } from 'ionic-angular'; | |
import { Firebase } from '@ionic-native/firebase'; | |
constructor( | |
... | |
private firebase: Firebase, | |
public platform: Platform, | |
//When the app's open, we'll show them as Toasts, but feel free to use an Alert instead | |
public toastCtrl: ToastController | |
){ | |
this.platform.ready().then(() => { | |
try{ | |
this.initializeFirebase(); | |
} catch (error) { | |
this.firebase.logError(error); | |
} | |
}); | |
} | |
initializeFirebase() { | |
if(!this.platform.is("core")) { | |
this.firebase.subscribe("all"); | |
this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS(); | |
} | |
} | |
initializeFirebaseAndroid() { | |
this.firebase.getToken().then(token => {}); | |
this.firebase.onTokenRefresh().subscribe(token => {}) | |
this.subscribeToPushNotifications(); | |
} | |
initializeFirebaseIOS() { | |
this.firebase.grantPermission() | |
.then(() => { | |
this.firebase.getToken().then(token => {}); | |
this.firebase.onTokenRefresh().subscribe(token => {}) | |
this.subscribeToPushNotifications(); | |
}) | |
.catch((error) => { | |
this.firebase.logError(error); | |
}); | |
} | |
subscribeToPushNotifications() { | |
this.firebase.onNotificationOpen().subscribe((response) => { | |
if(response.tap){ | |
//Received while app in background (this should be the callback when a system notification is tapped) | |
//This is empty for our app since we just needed the notification to open the app | |
}else{ | |
//received while app in foreground (show a toast) | |
let toast = this.toastCtrl.create({ | |
message: response.body, | |
duration: 3000 | |
}); | |
toast.present(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment