You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 7, 2019. It is now read-only.
@@ -6,6 +6,8 @@ The code for the Push Plugin for NativeScript.
6
6
-[API Reference](#api)
7
7
-[Troubleshooting](#troubleshooting)
8
8
-[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
+
9
11
10
12
11
13
## Getting started
@@ -16,26 +18,28 @@ The code for the Push Plugin for NativeScript.
16
18
17
19
or use an existing one.
18
20
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:
20
22
21
-
tns plugin add nativescript-push-notifications
23
+
tns plugin add nativescript-push-notifications
22
24
23
25
### Android
24
26
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
+
25
29
- Go to the application folder and add the Android platform to the application
26
30
27
31
tns platform add android
28
32
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):
30
34
31
35
```javascript
32
36
var pushPlugin =require("nativescript-push-notifications");
33
37
varself=this;
34
38
pushPlugin.register({ senderID:'<ENTER_YOUR_PROJECT_NUMBER>' }, function (data){
@@ -44,7 +48,7 @@ The code for the Push Plugin for NativeScript.
44
48
45
49
tns run android
46
50
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.
48
52
49
53
### iOS
50
54
@@ -120,7 +124,7 @@ The code for the Push Plugin for NativeScript.
## 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):
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:
> 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