Created
          October 3, 2018 12:53 
        
      - 
      
- 
        Save juanchoperezj/9ec677be402fc8755b00f3e7485f638d to your computer and use it in GitHub Desktop. 
    Trigger that notifies the javascript thread of a change occurred and sends push notifications using postgres and firebase
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | const admin = require('firebase-admin') | |
| const request = require('request') | |
| const config = require('../config') | |
| const {JWT} = require('google-auth-library') | |
| const dbConfig = config.database | |
| const pg = require('pg') | |
| /** | |
| * @type {string} | |
| */ | |
| const pgConnectionString = `postgres://${dbConfig.username}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}` | |
| /** | |
| */ | |
| exports.initializeListener = () => { | |
| const db = admin.database() | |
| console.log('initializeListener') | |
| const client = new pg.Client(pgConnectionString) | |
| client.connect() | |
| // Listen postgres NOTIFY | |
| client.query('LISTEN "watch_secrets"') | |
| // Callback when notification payload is fired | |
| // do your stuff here | |
| }) | |
| /** | |
| * @param token : String | |
| * @param param : number | |
| * @param notificationData : Object | |
| */ | |
| function sendNotificationV1(token, param, notificationData) { | |
| getAccessToken().then(accessToken => { | |
| console.log('accessToken', accessToken) | |
| request({ | |
| url: 'https://fcm.googleapis.com/v1/projects/your-project/messages:send', | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': ' application/json', | |
| 'Authorization': `Bearer ${accessToken}` | |
| }, | |
| body: JSON.stringify({ | |
| 'message': { | |
| 'token': token, | |
| 'notification': { | |
| 'title': 'title', | |
| 'body': `body` | |
| }, | |
| 'android': { | |
| 'ttl': '86400s', | |
| 'data': { | |
| channelId: 'channel_name', | |
| 'title': 'title', | |
| 'body': `body` | |
| }, | |
| 'notification': { | |
| 'sound': 'default' | |
| } | |
| }, | |
| 'apns': { | |
| 'headers': { | |
| 'apns-priority': '5' | |
| }, | |
| 'payload': { | |
| 'aps': { | |
| 'content-available': 1, | |
| 'sound': 'default', | |
| 'badge': 1 | |
| } | |
| } | |
| }, | |
| 'webpush': { | |
| 'headers': { | |
| 'TTL': '86400' | |
| }, | |
| 'notification': { | |
| //url | |
| click_action: config.mode === 'development' ? `dev` : `prod` | |
| } | |
| // 'fcm_options': { | |
| //url | |
| // link: config.mode === 'development' ? `dev` : `prod` | |
| // } | |
| } | |
| } | |
| }) | |
| }, (error, response, body) => { | |
| console.log(error) | |
| console.log(body) | |
| }) | |
| }) | |
| } | |
| /** | |
| * @returns {Promise<any>} | |
| */ | |
| function getAccessToken() { | |
| return new Promise((resolve, reject) => { | |
| const key = require(config.auth.firebase.keyPath) | |
| const scopes = ['https://www.googleapis.com/auth/firebase.messaging'] | |
| const client = new JWT( | |
| key.client_email, | |
| null, | |
| key.private_key, | |
| scopes, | |
| null | |
| ) | |
| client.authorize((err, tokens) => { | |
| if (err) { | |
| reject(err) | |
| return | |
| } | |
| resolve(tokens.access_token) | |
| }) | |
| }) | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment