Skip to content
This repository was archived by the owner on Feb 7, 2019. It is now read-only.

Commit 4b35673

Browse files
committed
Adding information about FCM configuration
1 parent 43bea90 commit 4b35673

1 file changed

Lines changed: 123 additions & 12 deletions

File tree

README.md

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The code for the Push Plugin for NativeScript.
66
- [API Reference](#api)
77
- [Troubleshooting](#troubleshooting)
88
- [Using with Telerik Backend Services](#using-with-telerik-backend-services)
9+
- [Android Configuration for using Firebase Cloud Messaging](#android-configuration-for-using-firebase-cloud-messaging)
10+
911

1012

1113
## Getting started
@@ -16,26 +18,28 @@ The code for the Push Plugin for NativeScript.
1618

1719
or use an existing one.
1820

19-
- Add the Push Plugin (from NPM). This will install the push plugin in node_module, in the root of the project. When adding a new platform (or using an existing one) the plugin will be added there as well. Go to the application folder and add the push plugin:
21+
- Add the Push Plugin (from NPM). This will install the push plugin in the `node_modules` folder, in the root of the project. When adding a new platform (or using an existing one) the plugin will be added there as well. Go to the application folder and add the push plugin:
2022

21-
tns plugin add nativescript-push-notifications
23+
tns plugin add nativescript-push-notifications
2224

2325
### Android
2426

27+
> See the [Android Configuration for using Firebase Cloud Messaging](#android-configuration-for-using-firebase-cloud-messaging) for information about how to add Firebase to your project.
28+
2529
- Go to the application folder and add the Android platform to the application
2630

2731
tns platform add android
2832

29-
- Add sample code in app/main-view-model.js in the function HelloWorldModel() like this one to subscribe and receive messages (Enter your google project number in the options of the register method):
33+
- Add sample code in app/main-view-model.js in the function HelloWorldModel() like this one to subscribe and receive messages (enter your Firebase Cloud Messaging **Sender ID** in the options of the register method):
3034

3135
```javascript
3236
var pushPlugin = require("nativescript-push-notifications");
3337
var self = this;
3438
pushPlugin.register({ senderID: '<ENTER_YOUR_PROJECT_NUMBER>' }, function (data){
3539
self.set("message", "" + JSON.stringify(data));
3640
}, function() { });
37-
38-
pushPlugin.onMessageReceived(function callback(data) {
41+
42+
pushPlugin.onMessageReceived(function callback(data) {
3943
self.set("message", "" + JSON.stringify(data));
4044
});
4145
```
@@ -44,7 +48,7 @@ The code for the Push Plugin for NativeScript.
4448

4549
tns run android
4650

47-
- The access token is written in the console and in the message area, after subscribing (Look for ObtainTokenThread log record). When sending a notification, the message below the TAP button should be changed with the message received.
51+
- The access token is written in the console and in the message area, after subscribing (Look for ObtainTokenThread log record). When sending a notification, the message below the TAP button should be changed with the message received.
4852

4953
### iOS
5054

@@ -120,7 +124,7 @@ The code for the Push Plugin for NativeScript.
120124
> register(settings, successCallback, errorCallback)
121125
122126
```javascript
123-
127+
124128
var settings = {
125129
// Android settings
126130
senderID: '<ENTER_YOUR_PROJECT_NUMBER>', // Android: Required setting with the sender/project number
@@ -139,7 +143,7 @@ The code for the Push Plugin for NativeScript.
139143
}
140144
};
141145

142-
146+
143147
pushPlugin.register(settings,
144148
// Success callback
145149
function(token) {
@@ -217,7 +221,7 @@ The code for the Push Plugin for NativeScript.
217221
}
218222
};
219223

220-
224+
221225
pushPlugin.register(settings,
222226
// Success callback
223227
function(token){
@@ -226,7 +230,7 @@ The code for the Push Plugin for NativeScript.
226230
if(pushPlugin.onMessageReceived) {
227231
pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
228232
}
229-
233+
230234
// Register the interactive settings
231235
if(settings.interactiveSettings) {
232236
pushPlugin.registerUserNotificationSettings(function() {
@@ -261,7 +265,7 @@ The code for the Push Plugin for NativeScript.
261265
> onTokenRefresh(callback)
262266
263267
```javascript
264-
268+
265269
pushPlugin.onTokenRefresh(function(token){
266270
alert(token);
267271
});
@@ -276,7 +280,7 @@ In case the application doesn't work as expected. Here are some things you can v
276280

277281
- Ensure that the AndroindManifest.xml located at platforms\android contains the following snippets for registering the GCM listener:
278282

279-
```xml
283+
```XML
280284
<activity android:name="com.telerik.pushplugin.PushHandlerActivity"/>
281285
<receiver
282286
android:name="com.google.android.gms.gcm.GcmReceiver"
@@ -313,3 +317,110 @@ In case the application doesn't work as expected. Here are some things you can v
313317
In order to use the plugin with Telerik Backend Services take a look at the official sample:
314318

315319
[Telerik Backend Services NativeScript Push Sample](https://github.com/NativeScript/sample-push-plugin)
320+
321+
## Android Configuration for using Firebase Cloud Messaging
322+
323+
From version **0.1.0** the `nativescript-push-notifications` module for Android relies on the Firebase Cloud Messaging (FCM) SDK. In the steps below you will be guided to complete a few additional steps to prepare your Android app to receive push notifications from FCM.
324+
325+
1. Add the FCM SDK
326+
327+
- Navigate to the project `platforms/android/` folder and locate the application-level `build.gradle` file
328+
- Add the `google-services` plugin to the list of other dependencies in your app's `build.gradle` file
329+
330+
```Groovy
331+
dependencies {
332+
// ...
333+
classpath "com.google.gms:google-services:3.0.0"
334+
// ...
335+
}
336+
```
337+
338+
- Add the following line be at the bottom of your `build.gradle` file to enable the Gradle plugin
339+
340+
```Groovy
341+
apply plugin: 'com.google.gms.google-services'
342+
```
343+
344+
1. Add the `google-services.json` file
345+
346+
To use FCM, you need this file. It contains configurations and credentials for your Firebase project. To obtain this follow the instructions for adding Firebase to your project from the official [documentation](https://firebase.google.com/docs/android/setup). Scroll down to the **Manually add Firebase** section.
347+
348+
Place the file in your app's `App_Resources/Android` folder
349+
350+
351+
1. Obtain the FCM Server Key
352+
353+
This key is required to be able to send programmatically push notifications to your app. You can obtain this key from your Firebase project.
354+
355+
If you are using the Telerik Platform Notifications service refer to this [article](http://docs.telerik.com/platform/backend-services/javascript/push-notifications/push-enabling#android-settings) for instructions how to set up this key.
356+
357+
### Receive and Handle Messages from FCM on Android
358+
359+
The plugin allows for handling **data**, **notification**, and messages that contain **both** payload keys which for the purposes of this article are reffered to as **mixed**. More specifics on these messages are explained [here](https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages).
360+
361+
The plugin extends the `FirebaseMessagingService` and overrides its `onMessageReceived` callback. In your app you need to use the `onMessageReceived(message, data, notification)` method of the NativeScript module.
362+
363+
The behavior of the `onMessageReceived` callback in the module follows the behavior of the FCM service.
364+
365+
#### Handling **Data** Messages
366+
367+
The `onMessageReceived` method of the plugin is called each time a `data` notification is received.
368+
369+
When in background mode, a notification is constructed according to the values of the key specified above and placed in the tray. Tapping the notification launches the app and invokes the `onMessageReceived` callback.
370+
371+
#### Handling **Notification** Messages
372+
373+
If the app is in foreground, it invokes the `onMessageReceived` callback with three arguments (message, data, notification).
374+
375+
If the app is in background, a notification is put in the tray. When tapped, it launches the app, but does not invoke the `onMessageReceived` callback.
376+
377+
#### Handling **Mixed** Messages
378+
379+
Mixed messages are messages that contain in their load both **data** and **notification** keys. When such message is received:
380+
381+
- The app is in foreground, the `onMessageReceived` callback is invoked with parameters (message, data)
382+
- The app is in background, the `onMessageReceived` callback is not invoked. A notification is placed in the system tray. If the notification in the tray is tapped, the `data` part of the mixed message is available in the extras of the intent of the activity and are available in the respective [application event](https://docs.nativescript.org/core-concepts/application-lifecycle) of NativeScript.
383+
384+
Example of handling the `data` part in the application *resume* event (e.g. the app was brought to the foreground from the notification):
385+
386+
```
387+
application.on(application.resumeEvent, function(args) {
388+
if (args.android) {
389+
var act = args.android;
390+
var intent = act.getIntent();
391+
var extras = intent.getExtras();
392+
if (extras) {
393+
// for (var key in extras) {
394+
// console.log(key + ' -> ' + extras[key]);
395+
// }
396+
var msg = extras.get('someKey');
397+
}
398+
}
399+
});
400+
```
401+
402+
#### Parameters of the onMessageReceived Callback
403+
404+
Depending on the notification event and payload, the `onMessageReceived` callback is invoked with up to three arguments.
405+
406+
* `message` - *String*. A string representation of the `data.message` value in the notification payload.
407+
* `data` - *Object*. A JSON representation of the `data` value in the notification payload.
408+
* `notification` - `RemoteMessage.Notification`. A representation of the `RemoteMessage.Notification` class which can be accessed according to its public methods. This parameter is available in case the callback was called from a message with a `notification` key in the payload.
409+
410+
#### Setting Notification Icon and Color
411+
412+
> From version 0.1.0 the module no longer adds as default a large icon of the notification because this was forcing developers to always use a large icon which is not the native behavior.
413+
414+
The plugin automatically handles some keys in the `data` object like `message`, `title`, `color`, `smallIcon`, `largeIcon` and uses them to construct a notification entry in the tray. More information on these keys is available in the documentation of the Telerik Platform Notifications service documentation [article](http://docs.telerik.com/platform/backend-services/javascript/push-notifications/send-and-target/push-send-target-examples).
415+
416+
Custom default color and icon for **notification** messages can be set in the `AndroidManifest.xml` inside the `application` directive:
417+
418+
```XML
419+
<meta-data
420+
android:name="com.google.firebase.messaging.default_notification_icon"
421+
android:resource="@drawable/ic_stat_ic_notification" />
422+
<meta-data
423+
android:name="com.google.firebase.messaging.default_notification_color"
424+
android:resource="@color/colorAccent" />
425+
```
426+
> For more info visit the [Edit the app manifest](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#edit-the-app-manifest) article.

0 commit comments

Comments
 (0)