Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 16.4: Add FCM logic to ChatsPage
  • Loading branch information
darkbasic committed Oct 16, 2017
1 parent d7b4a34 commit 8927c7d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/pages/chats/chats.ts
Expand Up @@ -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'
Expand All @@ -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();
}

Expand All @@ -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[]> {
Expand Down

0 comments on commit 8927c7d

Please sign in to comment.