Permalink
Browse files

Step 16.4: Add FCM logic to ChatsPage

  • Loading branch information...
darkbasic committed Jun 8, 2017
1 parent d7b4a34 commit 8927c7d7d6a0dc7b81fd53381cd73ed29ad37e63
Showing with 32 additions and 1 deletion.
  1. +32 −1 src/pages/chats/chats.ts
View
@@ -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<Chat[]> {

0 comments on commit 8927c7d

Please sign in to comment.