Skip to content

Instantly share code, notes, and snippets.

@hiepxanh
Created July 16, 2018 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiepxanh/2315886ee15c9a45ab5a304e35e1ffd2 to your computer and use it in GitHub Desktop.
Save hiepxanh/2315886ee15c9a45ab5a304e35e1ffd2 to your computer and use it in GitHub Desktop.
firebase clould message
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { TabsPage } from '../pages/tabs/tabs';
import { FCM } from '@ionic-native/fcm';
import { ApiProvider } from '../providers/api/api';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = TabsPage;
constructor(
platform: Platform,
statusBar: StatusBar,
splashScreen: SplashScreen,
private fcm: FCM,
private apiProvider: ApiProvider
) {
platform.ready().then(() => {
console.log('current platform:',platform.platforms())
console.log('platform is ios?',platform.is('ios'))
console.log('platform is android?',platform.is('android'))
if (platform.is('cordova')) {
splashScreen.hide();
statusBar.styleDefault();
statusBar.overlaysWebView(false);
statusBar.styleBlackOpaque();
statusBar.backgroundColorByHexString('#F9690E');
this.setupFirebase();
}
});
}
setupFirebase() {
this.fcm.subscribeToTopic('all');
this.fcm.getToken().then(token => {
// backend.registerToken(token);
this.apiProvider.registerToken(token).subscribe(res => console.log(`token ${token} registered to server! response:`,res))
});
this.fcm.onNotification().subscribe(data => {
alert('message received')
if(data.wasTapped) {
console.info("Received in background");
} else {
console.info("Received in foreground");
};
});
this.fcm.onTokenRefresh().subscribe(token => {
// backend.registerToken(token);
this.apiProvider.registerToken(token).subscribe(res => console.log(`token ${token} registered`,res))
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment