Skip to content

Commit 8927c7d

Browse files
committed
Step 16.4: Add FCM logic to ChatsPage
1 parent d7b4a34 commit 8927c7d

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

‎src/pages/chats/chats.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Observable, Subscriber } from 'rxjs';
77
import { MessagesPage } from '../messages/messages';
88
import { ChatsOptionsComponent } from './chats-options';
99
import { NewChatComponent } from './new-chat';
10+
import { FCM } from "@ionic-native/fcm";
1011

1112
@Component({
1213
templateUrl: 'chats.html'
@@ -20,7 +21,8 @@ export class ChatsPage implements OnInit {
2021
private popoverCtrl: PopoverController,
2122
private modalCtrl: ModalController,
2223
private alertCtrl: AlertController,
23-
private platform: Platform) {
24+
private platform: Platform,
25+
private fcm: FCM) {
2426
this.senderId = Meteor.userId();
2527
}
2628

@@ -35,6 +37,35 @@ export class ChatsPage implements OnInit {
3537
this.chats = this.findChats();
3638
});
3739
});
40+
41+
// Notifications
42+
if (this.platform.is('cordova')) {
43+
//this.fcm.subscribeToTopic('news');
44+
45+
this.fcm.getToken().then(token => {
46+
console.log("Registering FCM token on backend");
47+
MeteorObservable.call('saveFcmToken', token).subscribe({
48+
next: () => console.log("FCM Token saved"),
49+
error: err => console.error('Impossible to save FCM token: ', err)
50+
});
51+
});
52+
53+
this.fcm.onNotification().subscribe(data => {
54+
if (data.wasTapped) {
55+
console.log("Received FCM notification in background");
56+
} else {
57+
console.log("Received FCM notification in foreground");
58+
}
59+
});
60+
61+
this.fcm.onTokenRefresh().subscribe(token => {
62+
console.log("Updating FCM token on backend");
63+
MeteorObservable.call('saveFcmToken', token).subscribe({
64+
next: () => console.log("FCM Token updated"),
65+
error: err => console.error('Impossible to update FCM token: ' + err)
66+
});
67+
});
68+
}
3869
}
3970

4071
findChats(): Observable<Chat[]> {

0 commit comments

Comments
 (0)