From d9f15355ca8a2cca4c67e1c37f9057e5d00628a5 Mon Sep 17 00:00:00 2001 From: paulincai Date: Tue, 8 Sep 2020 09:00:41 +0400 Subject: [PATCH] No longer depend on RAIX deprecated packages --- README.md | 6 ++++++ lib/client/cordova.js | 30 +++++++++++++++++------------- lib/client/web.js | 15 +++++++++------ lib/server/internalMethods.js | 6 +++--- lib/server/notification.js | 6 +++--- lib/server/pushToDB.js | 6 ++++-- package.js | 10 +++++----- 7 files changed, 47 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index ddd4cb8..c781259 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/lib/client/cordova.js b/lib/client/cordova.js index ea6a304..e206a50 100755 --- a/lib/client/cordova.js +++ b/lib/client/cordova.js @@ -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']) @@ -15,6 +17,9 @@ const getService = () => { return 'unknown' } +const PushEventState = new EventEmitter() +function EventState () {} + class PushHandle extends EventState { constructor () { super() @@ -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 }) @@ -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 => { @@ -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, diff --git a/lib/client/web.js b/lib/client/web.js index 96236b4..f1ae442 100644 --- a/lib/client/web.js +++ b/lib/client/web.js @@ -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' @@ -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() @@ -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) @@ -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 @@ -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)) }) @@ -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.') } @@ -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) @@ -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, diff --git a/lib/server/internalMethods.js b/lib/server/internalMethods.js index f423ca6..7121650 100755 --- a/lib/server/internalMethods.js +++ b/lib/server/internalMethods.js @@ -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 @@ -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 ] }) } diff --git a/lib/server/notification.js b/lib/server/notification.js index 75a1c83..1d58924 100644 --- a/lib/server/notification.js +++ b/lib/server/notification.js @@ -39,7 +39,7 @@ const sendNotification = (isDebug, fcmConnections, defaults, userToken, mongoNot }, apns: { headers: { - 'apns-priority': defaults.apnsPriority, + 'apns-priority': defaults.apnsPriority }, payload: { aps: { @@ -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, @@ -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 diff --git a/lib/server/pushToDB.js b/lib/server/pushToDB.js index ad80281..acfa94d 100755 --- a/lib/server/pushToDB.js +++ b/lib/server/pushToDB.js @@ -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') @@ -106,4 +108,4 @@ Push.deny = rules => { } } -export { Push } \ No newline at end of file +export { Push } diff --git a/package.js b/package.js index 28d5e1a..f049b97 100755 --- a/package.js +++ b/package.js @@ -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({ @@ -28,8 +29,7 @@ Package.onUse(api => { 'check', 'mongo', 'ejson', - 'random', - 'raix:eventstate@0.0.5' + 'random' ], ['client', 'server']) api.use('mongo', 'server')