Socket
Socket
Sign inDemoInstall

nativescript-push-notifications

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nativescript-push-notifications - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

definitions.d.ts

21

package.json
{
"name" : "nativescript-push-notifications",
"version": "0.1.0",
"version": "0.1.1",
"main" : "push-plugin.js",

@@ -14,4 +14,19 @@ "repository": {

"android": "2.4.1"
}
}
},
"hooks": [
{
"type": "before-prepare",
"script": "hooks/before-prepare.js",
"inject": true
}
]
},
"scripts": {
"postinstall": "node postinstall.js",
"preuninstall": "node preuninstall.js"
},
"dependencies": {
"nativescript-hook": "0.2.1"
},
"typings": "./definitions.d.ts"
}

14

push-plugin.android.js
module.exports = (function () {
var app = require('application');
(function() {
// Hook on the application events
var registerLifecycleEvents = function () {
com.telerik.pushplugin.PushLifecycleCallbacks.registerCallbacks(app.android.nativeApp);
})();
};
// Hook on the application events
if (app.android.nativeApp) {
registerLifecycleEvents();
} else {
app.on(app.launchEvent, registerLifecycleEvents);
}
var pluginObject = {
register: function (options, successCallback, errorCallback) {
com.telerik.pushplugin.PushPlugin.register(app.android.context, options.senderID,
//Success
new com.telerik.pushplugin.PushPluginListener(

@@ -14,0 +18,0 @@ {

@@ -9,4 +9,6 @@ # Push Plugin for NativeScript

- [Using with Telerik Backend Services](#using-with-telerik-backend-services)
- [Android Configuration for using Firebase Cloud Messaging](#android-configuration-for-using-firebase-cloud-messaging)
## Getting started

@@ -20,8 +22,10 @@

- 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:
- 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:
tns plugin add nativescript-push-notifications
tns plugin add nativescript-push-notifications
### Android
> 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.
- Go to the application folder and add the Android platform to the application

@@ -31,3 +35,3 @@

- 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):
- 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):

@@ -40,4 +44,4 @@ ```javascript

}, function() { });
pushPlugin.onMessageReceived(function callback(data) {
pushPlugin.onMessageReceived(function callback(data) {
self.set("message", "" + JSON.stringify(data));

@@ -51,3 +55,3 @@ });

- 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.
- 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.

@@ -128,3 +132,3 @@ ### iOS

```javascript
var settings = {

@@ -148,3 +152,3 @@ // Android settings

pushPlugin.register(settings,

@@ -227,3 +231,3 @@ // Success callback

pushPlugin.register(settings,

@@ -237,3 +241,3 @@ // Success callback

}
// Register the interactive settings

@@ -273,3 +277,3 @@ if(settings.interactiveSettings) {

```javascript
pushPlugin.onTokenRefresh(function(token){

@@ -289,3 +293,3 @@ alert(token);

```xml
```XML
<activity android:name="com.telerik.pushplugin.PushHandlerActivity"/>

@@ -327,1 +331,108 @@ <receiver

[Telerik Backend Services NativeScript Push Sample](https://github.com/NativeScript/sample-push-plugin)
## Android Configuration for using Firebase Cloud Messaging
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.
1. Add the FCM SDK
- Navigate to the project `platforms/android/` folder and locate the application-level `build.gradle` file
- Add the `google-services` plugin to the list of other dependencies in your app's `build.gradle` file
```Groovy
dependencies {
// ...
classpath "com.google.gms:google-services:3.0.0"
// ...
}
```
- Add the following line be at the bottom of your `build.gradle` file to enable the Gradle plugin
```Groovy
apply plugin: 'com.google.gms.google-services'
```
1. Add the `google-services.json` file
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.
Place the file in your app's `App_Resources/Android` folder
1. Obtain the FCM Server Key
This key is required to be able to send programmatically push notifications to your app. You can obtain this key from your Firebase project.
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.
### Receive and Handle Messages from FCM on Android
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).
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.
The behavior of the `onMessageReceived` callback in the module follows the behavior of the FCM service.
#### Handling **Data** Messages
The `onMessageReceived` method of the plugin is called each time a `data` notification is received.
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.
#### Handling **Notification** Messages
If the app is in foreground, it invokes the `onMessageReceived` callback with three arguments (message, data, notification).
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.
#### Handling **Mixed** Messages
Mixed messages are messages that contain in their load both **data** and **notification** keys. When such message is received:
- The app is in foreground, the `onMessageReceived` callback is invoked with parameters (message, data)
- 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.
Example of handling the `data` part in the application *resume* event (e.g. the app was brought to the foreground from the notification):
```
application.on(application.resumeEvent, function(args) {
if (args.android) {
var act = args.android;
var intent = act.getIntent();
var extras = intent.getExtras();
if (extras) {
// for (var key in extras) {
// console.log(key + ' -> ' + extras[key]);
// }
var msg = extras.get('someKey');
}
}
});
```
#### Parameters of the onMessageReceived Callback
Depending on the notification event and payload, the `onMessageReceived` callback is invoked with up to three arguments.
* `message` - *String*. A string representation of the `data.message` value in the notification payload.
* `data` - *Object*. A JSON representation of the `data` value in the notification payload.
* `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.
#### Setting Notification Icon and Color
> 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.
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).
Custom default color and icon for **notification** messages can be set in the `AndroidManifest.xml` inside the `application` directive:
```XML
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
```
> For more info visit the [Edit the app manifest](https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#edit-the-app-manifest) article.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc