Guest User

Untitled

a guest
May 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import { Nav, Platform, ToastController } from 'ionic-angular';
  2. import { Firebase } from '@ionic-native/firebase';
  3. constructor(
  4. ...
  5. private firebase: Firebase,
  6. public platform: Platform,
  7. //When the app's open, we'll show them as Toasts, but feel free to use an Alert instead
  8. public toastCtrl: ToastController
  9. ){
  10. this.platform.ready().then(() => {
  11. try{
  12. this.initializeFirebase();
  13. } catch (error) {
  14. this.firebase.logError(error);
  15. }
  16. });
  17. }
  18. initializeFirebase() {
  19. if(!this.platform.is("core")) {
  20. this.firebase.subscribe("all");
  21. this.platform.is('android') ? this.initializeFirebaseAndroid() : this.initializeFirebaseIOS();
  22. }
  23. }
  24. initializeFirebaseAndroid() {
  25. this.firebase.getToken().then(token => {});
  26. this.firebase.onTokenRefresh().subscribe(token => {})
  27. this.subscribeToPushNotifications();
  28. }
  29. initializeFirebaseIOS() {
  30. this.firebase.grantPermission()
  31. .then(() => {
  32. this.firebase.getToken().then(token => {});
  33. this.firebase.onTokenRefresh().subscribe(token => {})
  34. this.subscribeToPushNotifications();
  35. })
  36. .catch((error) => {
  37. this.firebase.logError(error);
  38. });
  39. }
  40. subscribeToPushNotifications() {
  41. this.firebase.onNotificationOpen().subscribe((response) => {
  42. if(response.tap){
  43. //Received while app in background (this should be the callback when a system notification is tapped)
  44. //This is empty for our app since we just needed the notification to open the app
  45. }else{
  46. //received while app in foreground (show a toast)
  47. let toast = this.toastCtrl.create({
  48. message: response.body,
  49. duration: 3000
  50. });
  51. toast.present();
  52. }
  53. });
  54. }
Add Comment
Please, Sign In to add comment