diff --git a/src/pages/chats/chats.ts b/src/pages/chats/chats.ts index b47326d73..be421394c 100644 --- a/src/pages/chats/chats.ts +++ b/src/pages/chats/chats.ts @@ -7,6 +7,7 @@ import { Observable, Subscriber } from 'rxjs'; import { MessagesPage } from '../messages/messages'; import { ChatsOptionsComponent } from './chats-options'; import { NewChatComponent } from './new-chat'; +import { FCM } from "@ionic-native/fcm"; @Component({ templateUrl: 'chats.html' @@ -20,7 +21,8 @@ export class ChatsPage implements OnInit { private popoverCtrl: PopoverController, private modalCtrl: ModalController, private alertCtrl: AlertController, - private platform: Platform) { + private platform: Platform, + private fcm: FCM) { this.senderId = Meteor.userId(); } @@ -35,6 +37,35 @@ export class ChatsPage implements OnInit { this.chats = this.findChats(); }); }); + + // Notifications + if (this.platform.is('cordova')) { + //this.fcm.subscribeToTopic('news'); + + this.fcm.getToken().then(token => { + console.log("Registering FCM token on backend"); + MeteorObservable.call('saveFcmToken', token).subscribe({ + next: () => console.log("FCM Token saved"), + error: err => console.error('Impossible to save FCM token: ', err) + }); + }); + + this.fcm.onNotification().subscribe(data => { + if (data.wasTapped) { + console.log("Received FCM notification in background"); + } else { + console.log("Received FCM notification in foreground"); + } + }); + + this.fcm.onTokenRefresh().subscribe(token => { + console.log("Updating FCM token on backend"); + MeteorObservable.call('saveFcmToken', token).subscribe({ + next: () => console.log("FCM Token updated"), + error: err => console.error('Impossible to update FCM token: ' + err) + }); + }); + } } findChats(): Observable {