push-node-lib

1.0.6 • Public • Published

Push notification

The library supports sending messages from the app server to the push server

Set up user information with Push notification server

After the user login successfully, you need to send the user information to the push notification server using the following function:

var jwt = require('json-web-token');
var PushNotification = require('push-node-lib');
var setupUser = () =>{ 
    //The API_KEY of the project is encoded using the JSON Web Token that contains projectId    
    //The following statement takes the id of the current project and saves it to the projectId variable   
    var PROJECT_ID = jwt.decode(API_KEY).projectId;
    //The following command creates a push notification with the following parameters:    
    //id of project, url of push notification server, API_KEY of project    
    var pushNotification = new PushNotification(PROJECT_ID, NOTIFICATION_URL, API_KEY);    
    //Then call setupUser function with parameters:    
    //id of current user, true if user allow, false if now allow   
    //This function return the token of current user    
    return pushNotification.setupUser(USER_ID, true);
}

This function returns the token of the current user. You need to store this token in your browser's cookie as "token" for the client-side library. For example:

var token = setupUser();    
if (token != null) {      
    res.cookie('token', token);    
}

Send a notification to the user

var jwt = require('json-web-token');
var PushNotification = require('push-node-lib');
var sendToPushServer = () => {      
    //The API_KEY of the project is encoded using the JSON Web Token that contains projectId    
    //The following statement takes the id of the current project and saves it to the projectId variable    
    var PROJECT_ID = jwt.decode(API_KEY).projectId;    
    //The following command creates a push notification with the following parameters:
    //id of project, url of push notification server, API_KEY of project    
    var pushNotification = new PushNotification(PROJECT_ID, NOTIFICATION_URL, API_KEY);    
    //You then send the message using the sendMessage functions that the library supports    
    pushNotification.sendMessageToUser(MESSAGE, TITLE, LINK_ICON, LINK_WHEN_CLICK, CREATED_AT, UPDATED_AT, USER_RECEIVE_TOKEN);
}

This library supports 3 functions to send a message to the user:

  1. Send to 1 user: sendMessageToUser
  2. Send to a list users: sendMessageToListUser
  3. Send to all users of project: sendMessageToAll

Readme

Keywords

Package Sidebar

Install

npm i push-node-lib

Weekly Downloads

0

Version

1.0.6

License

MIT

Unpacked Size

15.3 kB

Total Files

4

Last publish

Collaborators

  • quanghd96