forked from Urigo/Ionic2CLI-Meteor-WhatsApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
708 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
In this step we are going to implement push notifications using Google's `Firebase Cloud Messaging` (`FCM`). Whenever a user will send you a message, if you don't have our application in the foreground you will get a push notification. | ||
|
||
First we will have to create `google-services.json` in our project's root directory: | ||
|
||
{{{diffStep 16.1}}} | ||
|
||
Then we need to install the `FCM` `Cordova` plug-in: | ||
|
||
$ ionic cordova plugin add cordova-plugin-fcm --save | ||
$ npm install --save @ionic-native/fcm | ||
|
||
Then let's add it to `app.module.ts`: | ||
|
||
{{{diffStep 16.3}}} | ||
|
||
Now we can start adding some `FCM` logic into `ChatsPage`: | ||
|
||
{{{diffStep 16.4}}} | ||
|
||
We used the `saveFcmToken` `Meteor` method, so we need to create it first: | ||
|
||
{{{diffStep 16.5}}} | ||
|
||
Since we will soon need the `node-fetch` package, we will need to install it first: | ||
|
||
$ npm install --save node-fetch | ||
$ npm install --save-dev @types/node-fetch | ||
|
||
Let's implement our server side service which will actually send the notification: | ||
|
||
{{{diffStep 16.7}}} | ||
|
||
Now we should edit the `AddMessage` `Meteor` method to use our just-created service to send the notification: | ||
|
||
{{{diffStep 16.8}}} | ||
|
||
Before the `Typescript` compiler complains, let's update our `models`: | ||
|
||
{{{diffStep 16.9}}} | ||
|
||
{{{navStep prevRef="https://angular-meteor.com/tutorials/whatsapp2/ionic/addressbook" nextRef="https://angular-meteor.com/tutorials/whatsapp2/ionic/facebook"}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,344 @@ | ||
# Step 16: FCM Push Notifications | ||
|
||
In this step we are going to implement push notifications using Google's `Firebase Cloud Messaging` (`FCM`). Whenever a user will send you a message, if you don't have our application in the foreground you will get a push notification. | ||
|
||
First we will have to create `google-services.json` in our project's root directory: | ||
|
||
[{]: <helper> (diffStep 16.1) | ||
|
||
#### [Step 16.1: Add google-services.json FCM config](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/9d0be02) | ||
|
||
##### Added google-services.json | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
<b>+┊ ┊ 1┊{</b> | ||
<b>+┊ ┊ 2┊ "project_info": {</b> | ||
<b>+┊ ┊ 3┊ "project_number": "152311690748",</b> | ||
<b>+┊ ┊ 4┊ "firebase_url": "https://meteor-c069e.firebaseio.com",</b> | ||
<b>+┊ ┊ 5┊ "project_id": "meteor-c069e",</b> | ||
<b>+┊ ┊ 6┊ "storage_bucket": "meteor-c069e.appspot.com"</b> | ||
<b>+┊ ┊ 7┊ },</b> | ||
<b>+┊ ┊ 8┊ "client": [</b> | ||
<b>+┊ ┊ 9┊ {</b> | ||
<b>+┊ ┊10┊ "client_info": {</b> | ||
<b>+┊ ┊11┊ "mobilesdk_app_id": "1:152311690748:android:25f0ec3806cf1f01",</b> | ||
<b>+┊ ┊12┊ "android_client_info": {</b> | ||
<b>+┊ ┊13┊ "package_name": "io.ionic.starter"</b> | ||
<b>+┊ ┊14┊ }</b> | ||
<b>+┊ ┊15┊ },</b> | ||
<b>+┊ ┊16┊ "oauth_client": [</b> | ||
<b>+┊ ┊17┊ {</b> | ||
<b>+┊ ┊18┊ "client_id": "152311690748-2ht8fdqhlnv8lsrrvnd7u521j9rcgi3h.apps.googleusercontent.com",</b> | ||
<b>+┊ ┊19┊ "client_type": 3</b> | ||
<b>+┊ ┊20┊ }</b> | ||
<b>+┊ ┊21┊ ],</b> | ||
<b>+┊ ┊22┊ "api_key": [</b> | ||
<b>+┊ ┊23┊ {</b> | ||
<b>+┊ ┊24┊ "current_key": "AIzaSyD9CKsY6bC_a4Equ2HpbcrSErgJ2pheDS4"</b> | ||
<b>+┊ ┊25┊ }</b> | ||
<b>+┊ ┊26┊ ],</b> | ||
<b>+┊ ┊27┊ "services": {</b> | ||
<b>+┊ ┊28┊ "analytics_service": {</b> | ||
<b>+┊ ┊29┊ "status": 1</b> | ||
<b>+┊ ┊30┊ },</b> | ||
<b>+┊ ┊31┊ "appinvite_service": {</b> | ||
<b>+┊ ┊32┊ "status": 1,</b> | ||
<b>+┊ ┊33┊ "other_platform_oauth_client": []</b> | ||
<b>+┊ ┊34┊ },</b> | ||
<b>+┊ ┊35┊ "ads_service": {</b> | ||
<b>+┊ ┊36┊ "status": 2</b> | ||
<b>+┊ ┊37┊ }</b> | ||
<b>+┊ ┊38┊ }</b> | ||
<b>+┊ ┊39┊ }</b> | ||
<b>+┊ ┊40┊ ],</b> | ||
<b>+┊ ┊41┊ "configuration_version": "1"</b> | ||
<b>+┊ ┊42┊}</b> | ||
</pre> | ||
|
||
[}]: # | ||
|
||
Then we need to install the `FCM` `Cordova` plug-in: | ||
|
||
$ ionic cordova plugin add git+https://github.com/darkbasic/cordova-plugin-fcm.git --save | ||
$ npm install --save @ionic-native/fcm | ||
|
||
Then let's add it to `app.module.ts`: | ||
|
||
[{]: <helper> (diffStep 16.3) | ||
|
||
#### [Step 16.3: Add FCM to app.module.ts](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/9f97f05) | ||
|
||
##### Changed src/app/app.module.ts | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊10┊10┊import { Camera } from '@ionic-native/camera'; | ||
┊11┊11┊import { Crop } from '@ionic-native/crop'; | ||
┊12┊12┊import { Contacts } from "@ionic-native/contacts"; | ||
<b>+┊ ┊13┊import { FCM } from "@ionic-native/fcm";</b> | ||
┊13┊14┊import { AgmCoreModule } from '@agm/core'; | ||
┊14┊15┊import { MomentModule } from 'angular2-moment'; | ||
┊15┊16┊import { ChatsPage } from '../pages/chats/chats'; | ||
</pre> | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊77┊78┊ SmsReceiver, | ||
┊78┊79┊ Camera, | ||
┊79┊80┊ Crop, | ||
<b>+┊ ┊81┊ Contacts,</b> | ||
<b>+┊ ┊82┊ FCM</b> | ||
┊81┊83┊ ] | ||
┊82┊84┊}) | ||
┊83┊85┊export class AppModule {} | ||
</pre> | ||
|
||
[}]: # | ||
|
||
Now we can start adding some `FCM` logic into `ChatsPage`: | ||
|
||
[{]: <helper> (diffStep 16.4) | ||
|
||
#### [Step 16.4: Add FCM logic to ChatsPage](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/604e560) | ||
|
||
##### Changed src/pages/chats/chats.ts | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊ 7┊ 7┊import { MessagesPage } from '../messages/messages'; | ||
┊ 8┊ 8┊import { ChatsOptionsComponent } from './chats-options'; | ||
┊ 9┊ 9┊import { NewChatComponent } from './new-chat'; | ||
<b>+┊ ┊10┊import { FCM } from "@ionic-native/fcm";</b> | ||
┊10┊11┊ | ||
┊11┊12┊@Component({ | ||
┊12┊13┊ templateUrl: 'chats.html' | ||
</pre> | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊20┊21┊ private popoverCtrl: PopoverController, | ||
┊21┊22┊ private modalCtrl: ModalController, | ||
┊22┊23┊ private alertCtrl: AlertController, | ||
<b>+┊ ┊24┊ private platform: Platform,</b> | ||
<b>+┊ ┊25┊ private fcm: FCM) {</b> | ||
┊24┊26┊ this.senderId = Meteor.userId(); | ||
┊25┊27┊ } | ||
┊26┊28┊ | ||
</pre> | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊35┊37┊ this.chats = this.findChats(); | ||
┊36┊38┊ }); | ||
┊37┊39┊ }); | ||
<b>+┊ ┊40┊</b> | ||
<b>+┊ ┊41┊ // Notifications</b> | ||
<b>+┊ ┊42┊ if (this.platform.is('cordova')) {</b> | ||
<b>+┊ ┊43┊ //this.fcm.subscribeToTopic('news');</b> | ||
<b>+┊ ┊44┊</b> | ||
<b>+┊ ┊45┊ this.fcm.getToken().then(token => {</b> | ||
<b>+┊ ┊46┊ console.log("Registering FCM token on backend");</b> | ||
<b>+┊ ┊47┊ MeteorObservable.call('saveFcmToken', token).subscribe({</b> | ||
<b>+┊ ┊48┊ next: () => console.log("FCM Token saved"),</b> | ||
<b>+┊ ┊49┊ error: err => console.error('Impossible to save FCM token: ', err)</b> | ||
<b>+┊ ┊50┊ });</b> | ||
<b>+┊ ┊51┊ });</b> | ||
<b>+┊ ┊52┊</b> | ||
<b>+┊ ┊53┊ this.fcm.onNotification().subscribe(data => {</b> | ||
<b>+┊ ┊54┊ if (data.wasTapped) {</b> | ||
<b>+┊ ┊55┊ console.log("Received FCM notification in background");</b> | ||
<b>+┊ ┊56┊ } else {</b> | ||
<b>+┊ ┊57┊ console.log("Received FCM notification in foreground");</b> | ||
<b>+┊ ┊58┊ }</b> | ||
<b>+┊ ┊59┊ });</b> | ||
<b>+┊ ┊60┊</b> | ||
<b>+┊ ┊61┊ this.fcm.onTokenRefresh().subscribe(token => {</b> | ||
<b>+┊ ┊62┊ console.log("Updating FCM token on backend");</b> | ||
<b>+┊ ┊63┊ MeteorObservable.call('saveFcmToken', token).subscribe({</b> | ||
<b>+┊ ┊64┊ next: () => console.log("FCM Token updated"),</b> | ||
<b>+┊ ┊65┊ error: err => console.error('Impossible to update FCM token: ' + err)</b> | ||
<b>+┊ ┊66┊ });</b> | ||
<b>+┊ ┊67┊ });</b> | ||
<b>+┊ ┊68┊ }</b> | ||
┊38┊69┊ } | ||
┊39┊70┊ | ||
┊40┊71┊ findChats(): Observable<Chat[]> { | ||
</pre> | ||
|
||
[}]: # | ||
|
||
We used the `saveFcmToken` `Meteor` method, so we need to create it first: | ||
|
||
[{]: <helper> (diffStep 16.5) | ||
|
||
#### [Step 16.5: Create the saveFcmToken Meteor method](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/905defc) | ||
|
||
##### Changed api/server/methods.ts | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊2┊2┊import { Messages } from './collections/messages'; | ||
┊3┊3┊import { MessageType, Profile } from './models'; | ||
┊4┊4┊import { check, Match } from 'meteor/check'; | ||
<b>+┊ ┊5┊import { Users } from "./collections/users";</b> | ||
┊5┊6┊ | ||
┊6┊7┊const nonEmptyString = Match.Where((str) => { | ||
┊7┊8┊ check(str, String); | ||
</pre> | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊ 94┊ 95┊ }, | ||
┊ 95┊ 96┊ countMessages(): number { | ||
┊ 96┊ 97┊ return Messages.collection.find().count(); | ||
<b>+┊ ┊ 98┊ },</b> | ||
<b>+┊ ┊ 99┊ saveFcmToken(token: string): void {</b> | ||
<b>+┊ ┊100┊ if (!this.userId) throw new Meteor.Error('unauthorized', 'User must be logged-in to call this method');</b> | ||
<b>+┊ ┊101┊</b> | ||
<b>+┊ ┊102┊ check(token, nonEmptyString);</b> | ||
<b>+┊ ┊103┊</b> | ||
<b>+┊ ┊104┊ Users.collection.update({_id: this.userId}, {$set: {"fcmToken": token}});</b> | ||
┊ 97┊105┊ } | ||
┊ 98┊106┊}); | ||
</pre> | ||
|
||
[}]: # | ||
|
||
Since we will soon need the `node-fetch` package, we will need to install it first: | ||
|
||
$ npm install --save node-fetch | ||
$ npm install --save-dev @types/node-fetch | ||
|
||
Let's implement our server side service which will actually send the notification: | ||
|
||
[{]: <helper> (diffStep 16.7) | ||
|
||
#### [Step 16.7: Store credentials in settings.json](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/16edda7) | ||
|
||
##### Changed api/private/settings.json | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊ 4┊ 4┊ "verificationRetriesWaitTime": 0, | ||
┊ 5┊ 5┊ "adminPhoneNumbers": ["+9721234567", "+97212345678", "+97212345679"], | ||
┊ 6┊ 6┊ "phoneVerificationMasterCode": "1234" | ||
<b>+┊ ┊ 7┊ },</b> | ||
<b>+┊ ┊ 8┊ "private": {</b> | ||
<b>+┊ ┊ 9┊ "fcm": {</b> | ||
<b>+┊ ┊10┊ "key": "AIzaSyBnmvN5WNv3rAaLra1RUr9vA5k0pNp0KuY"</b> | ||
<b>+┊ ┊11┊ }</b> | ||
┊ 7┊12┊ } | ||
┊ 8┊13┊} | ||
</pre> | ||
|
||
[}]: # | ||
|
||
Now we should edit the `AddMessage` `Meteor` method to use our just-created service to send the notification: | ||
|
||
[{]: <helper> (diffStep 16.8) | ||
|
||
#### [Step 16.8: Create server side fcm service](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/82697f5) | ||
|
||
##### Added api/server/services/fcm.ts | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
<b>+┊ ┊ 1┊import fetch from 'node-fetch';</b> | ||
<b>+┊ ┊ 2┊</b> | ||
<b>+┊ ┊ 3┊export interface FcmNotification {</b> | ||
<b>+┊ ┊ 4┊ title: string;</b> | ||
<b>+┊ ┊ 5┊ text: string;</b> | ||
<b>+┊ ┊ 6┊}</b> | ||
<b>+┊ ┊ 7┊</b> | ||
<b>+┊ ┊ 8┊export class FcmService {</b> | ||
<b>+┊ ┊ 9┊ private key: string = Meteor.settings.private.fcm.key;</b> | ||
<b>+┊ ┊10┊</b> | ||
<b>+┊ ┊11┊ sendNotification(notification: FcmNotification, destination: string) {</b> | ||
<b>+┊ ┊12┊ const body = {</b> | ||
<b>+┊ ┊13┊ notification: notification,</b> | ||
<b>+┊ ┊14┊ to: destination</b> | ||
<b>+┊ ┊15┊ };</b> | ||
<b>+┊ ┊16┊</b> | ||
<b>+┊ ┊17┊ const options = {</b> | ||
<b>+┊ ┊18┊ method: 'POST',</b> | ||
<b>+┊ ┊19┊ body: JSON.stringify(body),</b> | ||
<b>+┊ ┊20┊ headers: {</b> | ||
<b>+┊ ┊21┊ "Content-Type": "application/json",</b> | ||
<b>+┊ ┊22┊ Authorization: `key=${this.key}`</b> | ||
<b>+┊ ┊23┊ },</b> | ||
<b>+┊ ┊24┊ };</b> | ||
<b>+┊ ┊25┊</b> | ||
<b>+┊ ┊26┊ return fetch("https://fcm.googleapis.com/fcm/send", options);</b> | ||
<b>+┊ ┊27┊ }</b> | ||
<b>+┊ ┊28┊}</b> | ||
<b>+┊ ┊29┊</b> | ||
<b>+┊ ┊30┊export const fcmService = new FcmService();</b> | ||
</pre> | ||
|
||
[}]: # | ||
|
||
Before the `Typescript` compiler complains, let's update our `models`: | ||
|
||
[{]: <helper> (diffStep 16.9) | ||
|
||
#### [Step 16.9: Update addMessage Meteor method to send fcm notifications](https://github.com/Urigo/Ionic2CLI-Meteor-WhatsApp/commit/60fee0e) | ||
|
||
##### Changed api/server/methods.ts | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊3┊3┊import { MessageType, Profile } from './models'; | ||
┊4┊4┊import { check, Match } from 'meteor/check'; | ||
┊5┊5┊import { Users } from "./collections/users"; | ||
<b>+┊ ┊6┊import { fcmService } from "./services/fcm";</b> | ||
┊6┊7┊ | ||
┊7┊8┊const nonEmptyString = Match.Where((str) => { | ||
┊8┊9┊ check(str, String); | ||
</pre> | ||
<pre> | ||
<i>╔══════╗</i> | ||
<i>║ diff ║</i> | ||
<i>╚══════╝</i> | ||
┊ 83┊ 84┊ 'Chat doesn\'t exist'); | ||
┊ 84┊ 85┊ } | ||
┊ 85┊ 86┊ | ||
<b>+┊ ┊ 87┊ const userId = this.userId;</b> | ||
<b>+┊ ┊ 88┊ const senderName = Users.collection.findOne({_id: userId}).profile.name;</b> | ||
<b>+┊ ┊ 89┊ const memberIds = Chats.collection.findOne({_id: chatId}).memberIds;</b> | ||
<b>+┊ ┊ 90┊ const tokens: string[] = Users.collection.find(</b> | ||
<b>+┊ ┊ 91┊ {</b> | ||
<b>+┊ ┊ 92┊ _id: {$in: memberIds, $nin: [userId]},</b> | ||
<b>+┊ ┊ 93┊ fcmToken: {$exists: true}</b> | ||
<b>+┊ ┊ 94┊ }</b> | ||
<b>+┊ ┊ 95┊ ).map((el) => el.fcmToken);</b> | ||
<b>+┊ ┊ 96┊</b> | ||
<b>+┊ ┊ 97┊ for (let token of tokens) {</b> | ||
<b>+┊ ┊ 98┊ console.log("Sending FCM notification");</b> | ||
<b>+┊ ┊ 99┊ fcmService.sendNotification({"title": `New message from ${senderName}`, "text": content}, token);</b> | ||
<b>+┊ ┊100┊ }</b> | ||
<b>+┊ ┊101┊</b> | ||
┊ 86┊102┊ return { | ||
┊ 87┊103┊ messageId: Messages.collection.insert({ | ||
┊ 88┊104┊ chatId: chatId, | ||
</pre> | ||
|
||
[}]: # | ||
|
||
[{]: <helper> (navStep nextRef="https://angular-meteor.com/tutorials/whatsapp2/ionic/facebook" prevRef="https://angular-meteor.com/tutorials/whatsapp2/ionic/addressbook") | ||
|
||
⟸ <a href="https://angular-meteor.com/tutorials/whatsapp2/ionic/addressbook">PREVIOUS STEP</a> <b>║</b> <a href="https://angular-meteor.com/tutorials/whatsapp2/ionic/facebook">NEXT STEP</a> ⟹ | ||
|
||
[}]: # | ||
|
Oops, something went wrong.