Skip to content

Commit

Permalink
No longer depend on RAIX deprecated packages
Browse files Browse the repository at this point in the history
  • Loading branch information
paulincai committed Sep 8, 2020
1 parent da764b1 commit d9f1535
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 32 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ This was tested with:
* firebase-admin: 8.6.1
* phonegap-plugin-push 2.3.0 (fixes the IOS 13 change of tokens issue)
* cordova-plugin-device 2.0.2


Google release notes for libraries used by activitree:push:

https://firebase.google.com/support/release-notes/admin/node
https://firebase.google.com/support/release-notes/js
30 changes: 17 additions & 13 deletions lib/client/cordova.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global device: false */
/* global PushNotification: false, EventState, EJSON, Package */
/* global PushNotification: false, EJSON, Package */

import { Meteor } from 'meteor/meteor'
import { Tracker } from 'meteor/tracker'
import EventEmitter from 'events'

const deviceStorage = window.localStorage
const addUserId = Boolean(Package['accounts-base'])
Expand All @@ -15,6 +17,9 @@ const getService = () => {
return 'unknown'
}

const PushEventState = new EventEmitter()
function EventState () {}

class PushHandle extends EventState {
constructor () {
super()
Expand All @@ -29,7 +34,7 @@ class PushHandle extends EventState {
this.push.setApplicationIconBadgeNumber(() => {
this.log('PushHandle.setBadge: was set to', count)
}, (e) => {
this.emit('error', {
PushEventState.emit('error', {
type: getService() + '.cordova',
error: 'PushHandle.setBadge Error: ' + e.message
})
Expand Down Expand Up @@ -73,9 +78,9 @@ class PushHandle extends EventState {
if (data && data.registrationId && oldToken !== data.registrationId) {
const token = { [getService()]: data.registrationId }
this.log('PushHandle.Token:', token)
this.emitState('token', token)
PushEventState.emit('token', token)
}
this.emitState('registration', ...arguments)
PushEventState.emit('registration', ...arguments)
})

this.push.on('notification', data => {
Expand All @@ -96,47 +101,46 @@ class PushHandle extends EventState {

// Emit alert event - this requires the app to be in foreground
if (data.message && data.additionalData.foreground) {
this.emit('alert', data)
PushEventState.emit('alert', data)
}

// Emit sound event
if (data.sound) {
this.emit('sound', data)
PushEventState.emit('sound', data)
}

// Emit badge event
if (typeof data.count !== 'undefined') {
this.log('PushHandle.SettingBadge:', data.count)
this.setBadge(data.count)
this.emit('badge', data)
PushEventState.emit('badge', data)
}

if (data.additionalData.foreground) {
this.log('PushHandle.Message: Got message while app is open:', data)
// TODO handle this
this.emit('message', data)
PushEventState.emit('message', data)
} else {
this.log('PushHandle.Startup: Got message while app was closed/in background:', data)
this.emitState('startup', data)
PushEventState.emit('startup', data)
}
this.emitState()
})

this.push.on('error', e => {
this.log('PushHandle.Error:', e)
this.emit('error', {
PushEventState.emit('error', {
type: getService() + '.cordova',
error: e.message
})
})

this.emitState('ready')
PushEventState.emit('ready')
}
})

const initPushUpdates = appName => {
Meteor.startup(() => {
CordovaPush.on('token', token => {
PushEventState.on('token', token => {
this.log('Got token:', token)
const data = {
token,
Expand Down
15 changes: 9 additions & 6 deletions lib/client/web.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* globals Notification */
import firebase from 'firebase/app'
import '@firebase/messaging'
import EventEmitter from 'events'

/* globals EventState, Package */
/* globals Package */
import { Meteor } from 'meteor/meteor'
import { Tracker } from 'meteor/tracker'

Expand All @@ -24,6 +25,9 @@ const interpretError = err => {
let pushSupported = false
let noConfiguration = true

const WebPushEventState = new EventEmitter()
function EventState () {}

class WebPushHandle extends EventState {
constructor () {
super()
Expand Down Expand Up @@ -81,7 +85,6 @@ class WebPushHandle extends EventState {
if (!noConfiguration && pushSupported) {
const webApp = firebase.initializeApp(configuration.firebase)
deviceStorage.setItem('pushSupport', true)

this.messaging = webApp.messaging()
this.messaging.usePublicVapidKey(configuration.publicVapidKey)

Expand All @@ -97,6 +100,7 @@ class WebPushHandle extends EventState {
/**
* Listen for the foreground messages. Background messages will be dealt with by the worker
*/

this.messaging.onMessage(payload => {
this.log('My Foreground Message payload here: ', payload)
const title = payload.notification.title
Expand All @@ -116,7 +120,7 @@ class WebPushHandle extends EventState {
// Delete the old Push from MongoDB and from the localStore
this.unsubscribe()
this.messaging.getToken().then(token => {
this.emitState('token', token)
WebPushEventState.emit('token', token)
}).catch(err => {
deviceStorage.setItem('lastSubscriptionMessage', interpretError(err))
})
Expand All @@ -140,7 +144,7 @@ class WebPushHandle extends EventState {
this.messaging.getToken().then(token => {
this.log('Calling subscribe')
if (token) {
this.emitState('token', token)
WebPushEventState.emit('token', token)
} else {
deviceStorage.setItem('lastSubscriptionMessage', 'No Instance ID token available. Request permission to generate one.')
}
Expand All @@ -156,7 +160,6 @@ class WebPushHandle extends EventState {
this.unsubscribe = () => {
const pushTokenId = deviceStorage.getItem('pushTokenId')
if (pushTokenId && deviceStorage.getItem('pushSupport')) {
this.log('I call an unsubscription to Push')
Meteor.call('push-unsub-webpush', pushTokenId, err => {
if (err) {
this.log('Could not save this token to DB: ', err)
Expand All @@ -173,7 +176,7 @@ class WebPushHandle extends EventState {

const initPushUpdates = appName => {
Meteor.startup(() => {
WebPush.on('token', token => {
WebPushEventState.on('token', token => {
const data = {
token: { web: token },
appName,
Expand Down
6 changes: 3 additions & 3 deletions lib/server/internalMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Meteor.methods({
token: matchToken,
appName: String,
userId: Match.OneOf(String, null),
metadata: Match.Optional(Object),
metadata: Match.Optional(Object)
})

// The if user id is set then user id should match on client and connection
Expand All @@ -37,9 +37,9 @@ Meteor.methods({
if (!doc) {
doc = Push.appCollection.findOne({
$and: [
{ token: options.token }, // Match token
{ token: options.token }, // Match token
{ appName: options.appName }, // Match appName
{ token: { $exists: true } } // Make sure token exists
{ token: { $exists: true } } // Make sure token exists
]
})
}
Expand Down
6 changes: 3 additions & 3 deletions lib/server/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const sendNotification = (isDebug, fcmConnections, defaults, userToken, mongoNot
},
apns: {
headers: {
'apns-priority': defaults.apnsPriority,
'apns-priority': defaults.apnsPriority
},
payload: {
aps: {
Expand Down Expand Up @@ -67,7 +67,7 @@ const sendNotification = (isDebug, fcmConnections, defaults, userToken, mongoNot
webpush: {
headers: {
Urgency: 'high',
TTL: defaults.webTTL, // mandatory, in seconds
TTL: defaults.webTTL // mandatory, in seconds
},
data: {
...defaults.data,
Expand All @@ -86,7 +86,7 @@ const sendNotification = (isDebug, fcmConnections, defaults, userToken, mongoNot
action: mongoNote.action || defaults.action,
title: 'Book Appointment'
}
]*/
] */
}, // Can take valued from here: https://developer.mozilla.org/en-US/docs/Web/API/Notification.
fcm_options: {
link: mongoNote.action || defaults.action
Expand Down
6 changes: 4 additions & 2 deletions lib/server/pushToDB.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// This is the match pattern for tokens
/* globals EventState */
import { Meteor } from 'meteor/meteor'
import { Mongo } from 'meteor/mongo'
import { Match, check } from 'meteor/check'

function EventState () {}

const Push = new EventState()

Push.notifications = new Mongo.Collection('_push_notifications')
Expand Down Expand Up @@ -106,4 +108,4 @@ Push.deny = rules => {
}
}

export { Push }
export { Push }
10 changes: 5 additions & 5 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/* globals Package, Npm, Cordova */
Package.describe({
name: 'activitree:push',
version: '2.0.7',
version: '2.0.8',
summary: 'Push Notifications for Cordova and Web/PWA with Firebase (FCM).',
git: 'https://github.com/activitree/meteor-push.git'
})

Npm.depends({
'firebase-admin': '8.12.1',
firebase: '7.15.0'
'firebase-admin': '8.13.0',
firebase: '7.17.2',
events: '3.2.0'
})

Cordova.depends({
Expand All @@ -28,8 +29,7 @@ Package.onUse(api => {
'check',
'mongo',
'ejson',
'random',
'raix:[email protected]'
'random'
], ['client', 'server'])

api.use('mongo', 'server')
Expand Down

0 comments on commit d9f1535

Please sign in to comment.