diff --git a/package-lock.json b/package-lock.json index a031b765..7c75795e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.5", "hasInstallScript": true, "dependencies": { + "@liamcottle/push-receiver": "^0.0.4", "@tailwindcss/forms": "^0.2.1", "@tailwindcss/postcss7-compat": "^2.0.2", "autoprefixer": "^9", @@ -20,7 +21,6 @@ "long": "^4.0.0", "postcss": "^7", "protobufjs": "^6.10.2", - "push-receiver": "^2.1.1", "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.0.2", "uuid": "^8.3.2", "vue": "^2.6.11", @@ -2109,6 +2109,60 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@liamcottle/push-receiver": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/@liamcottle/push-receiver/-/push-receiver-0.0.4.tgz", + "integrity": "sha512-BzgNJieg4oLNxHNKxH/arRh05QaKFAlAP52SmjyYMfa5beechoojXvhtzNe7aCmIuqCaVfiekaRYvZxX3SIYMA==", + "dependencies": { + "axios": "^1.7.7", + "http_ece": "^1.0.5", + "long": "^3.2.0", + "protobufjs": "^6.8.0", + "request": "^2.81.0", + "request-promise": "^4.2.1", + "uuid": "^3.1.0" + } + }, + "node_modules/@liamcottle/push-receiver/node_modules/axios": { + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/@liamcottle/push-receiver/node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/@liamcottle/push-receiver/node_modules/long": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz", + "integrity": "sha512-ZYvPPOMqUwPoDsbJaR10iQJYnMuZhRTvHYl62ErLIEX7RgFlziSBUUvrt3OVfc47QlHHpzPZYP17g3Fv7oeJkg==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/@liamcottle/push-receiver/node_modules/uuid": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", + "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", + "deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.", + "bin": { + "uuid": "bin/uuid" + } + }, "node_modules/@malept/cross-spawn-promise": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/@malept/cross-spawn-promise/-/cross-spawn-promise-1.1.1.tgz", @@ -14479,6 +14533,11 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", @@ -14616,36 +14675,6 @@ "node": "^10 || ^12 || >=14" } }, - "node_modules/push-receiver": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/push-receiver/-/push-receiver-2.1.1.tgz", - "integrity": "sha512-2f+Rglq6B+J6x06JEypDkm48vGf0lPCSjqO/+1F7Pg/8gippvZRSb/vloWuQlWGJw8ulma7lgPei34HAnJba5w==", - "dependencies": { - "http_ece": "^1.0.5", - "long": "^3.2.0", - "protobufjs": "^6.8.0", - "request": "^2.81.0", - "request-promise": "^4.2.1", - "uuid": "^3.1.0" - } - }, - "node_modules/push-receiver/node_modules/long": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/long/-/long-3.2.0.tgz", - "integrity": "sha512-ZYvPPOMqUwPoDsbJaR10iQJYnMuZhRTvHYl62ErLIEX7RgFlziSBUUvrt3OVfc47QlHHpzPZYP17g3Fv7oeJkg==", - "engines": { - "node": ">=0.6" - } - }, - "node_modules/push-receiver/node_modules/uuid": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", - "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", - "deprecated": "Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.", - "bin": { - "uuid": "bin/uuid" - } - }, "node_modules/q": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz", diff --git a/package.json b/package.json index 93bc84f5..829701ef 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "postuninstall": "electron-builder install-app-deps" }, "dependencies": { + "@liamcottle/push-receiver": "^0.0.4", "@tailwindcss/forms": "^0.2.1", "@tailwindcss/postcss7-compat": "^2.0.2", "autoprefixer": "^9", @@ -25,7 +26,6 @@ "long": "^4.0.0", "postcss": "^7", "protobufjs": "^6.10.2", - "push-receiver": "^2.1.1", "tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.0.2", "uuid": "^8.3.2", "vue": "^2.6.11", diff --git a/src/App.vue b/src/App.vue index 577d2b79..4320b18a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -282,14 +282,14 @@ export default { // configure expo data var deviceId = window.DataStore.Config.getExpoDeviceId(); - var experienceId = '@facepunch/RustCompanion'; + var projectId = '49451aca-a822-41e6-ad59-955718d0ff9c'; var appId = 'com.facepunch.rust.companion'; var fcmToken = window.DataStore.FCM.getCredentials().fcm.token; // register expo token this.expoStatus = Status.NOT_READY; this.expoStatusMessage = "Registering..."; - this.expoPushTokenReceiver.register(deviceId, experienceId, appId, fcmToken); + this.expoPushTokenReceiver.register(deviceId, projectId, appId, fcmToken); }, @@ -303,29 +303,23 @@ export default { // save persistent id to data store window.DataStore.FCM.addPersistentId(data.persistentId); - // make sure notification exists - var notification = data.notification; - if(!notification){ - console.log("notification is null!"); + // make sure app data exists + var appData = data.appData; + if(!appData){ + console.log("FCM notification appData is null!"); return; } - // make sure notification has data - if(!notification.data){ - console.log("notification has no data!"); - console.log(notification); + // make sure app data has body + const body = appData.find((item) => item.key === "body"); + if(!body){ + console.log("FCM notification appData has no body!"); + console.log(appData); return; } - // make sure notification has body - if(!notification.data.body){ - console.log("notification has no body!"); - console.log(notification); - return; - } - - // parse notification - var notificationBody = JSON.parse(notification.data.body); + // parse body + var notificationBody = JSON.parse(body.value); // make sure body has type if(!notificationBody.type){ @@ -430,7 +424,7 @@ export default { // register for a new set of fcm credentials this.fcmStatus = "Registering..."; - this.fcmNotificationReceiver.register('976529667804'); + this.fcmNotificationReceiver.register(); } diff --git a/src/js/ipc/main/ExpoPushTokenManager.js b/src/js/ipc/main/ExpoPushTokenManager.js index 8c1f2909..3b097445 100644 --- a/src/js/ipc/main/ExpoPushTokenManager.js +++ b/src/js/ipc/main/ExpoPushTokenManager.js @@ -40,12 +40,12 @@ class ExpoPushTokenManager { // register with expo axios.post('https://exp.host/--/api/v2/push/getExpoPushToken', { + type: data.type, deviceId: data.deviceId, - experienceId: data.experienceId, + development: data.development, appId: data.appId, deviceToken: data.deviceToken, - type: data.type, - development: data.development, + projectId: data.projectId, }, { /** diff --git a/src/js/ipc/main/FCMNotificationManager.js b/src/js/ipc/main/FCMNotificationManager.js index 4a46b750..1c6bcec0 100644 --- a/src/js/ipc/main/FCMNotificationManager.js +++ b/src/js/ipc/main/FCMNotificationManager.js @@ -1,4 +1,7 @@ -const {register, listen} = require('push-receiver'); +const { + AndroidFCM, + Client: PushReceiverClient, +} = require('@liamcottle/push-receiver'); /** * This class is responsible for registering a new android device with fcm @@ -42,11 +45,8 @@ class FCMNotificationManager { event.sender.send('push-receiver.notifications.listen.stopped'); } - onNotificationReceived(event, notification, persistentId) { - event.sender.send('push-receiver.notifications.received', { - 'notification': notification, - 'persistentId': persistentId, - }); + onNotificationReceived(event, data) { + event.sender.send('push-receiver.notifications.received', data); } onNotificationError(event, error) { @@ -64,7 +64,7 @@ class FCMNotificationManager { try { // register with gcm/fcm - const credentials = await register(data.senderId); + const credentials = await AndroidFCM.register(data.apiKey, data.projectId, data.gcmSenderId, data.gmsAppId, data.androidPackageName, data.androidPackageCert); // registering was successful this.onRegisterSuccess(event, credentials); @@ -90,13 +90,18 @@ class FCMNotificationManager { let persistentIds = data.persistentIds || []; // start listening for notifications - this.notificationClient = await listen({...credentials, persistentIds}, ({notification, persistentId}) => { + const androidId = credentials.gcm.androidId; + const securityToken = credentials.gcm.securityToken; + const client = new PushReceiverClient(androidId, securityToken, persistentIds); + client.on('ON_DATA_RECEIVED', (data) => { // notification was received - this.onNotificationReceived(event, notification, persistentId); + this.onNotificationReceived(event, data); }); + client.connect(); + // listening for notifications this.onNotificationListenStart(event); diff --git a/src/js/ipc/main/RustCompanionManager.js b/src/js/ipc/main/RustCompanionManager.js index 5ba929a1..54cbe64d 100644 --- a/src/js/ipc/main/RustCompanionManager.js +++ b/src/js/ipc/main/RustCompanionManager.js @@ -40,7 +40,7 @@ class RustCompanionManager { axios.post('https://companion-rust.facepunch.com:443/api/push/register', { AuthToken: data.token, DeviceId: data.deviceId, - PushKind: 0, + PushKind: 3, PushToken: data.expoPushToken, }).then((response) => { diff --git a/src/js/ipc/renderer/ExpoPushTokenReceiver.js b/src/js/ipc/renderer/ExpoPushTokenReceiver.js index 2e13b081..7baade60 100644 --- a/src/js/ipc/renderer/ExpoPushTokenReceiver.js +++ b/src/js/ipc/renderer/ExpoPushTokenReceiver.js @@ -37,14 +37,14 @@ class ExpoPushTokenReceiver extends EventEmitter { * - register.success * - register.error */ - register(deviceId, experienceId, appId, fcmToken) { + register(deviceId, projectId, appId, fcmToken) { ipcRenderer.send('expo-push-token.register', { + type: 'fcm', deviceId: deviceId, - experienceId: experienceId, + development: false, appId: appId, deviceToken: fcmToken, - type: 'fcm', - development: false, + projectId: projectId, }); } diff --git a/src/js/ipc/renderer/FCMNotificationReceiver.js b/src/js/ipc/renderer/FCMNotificationReceiver.js index f03ebeee..d0b3c37b 100644 --- a/src/js/ipc/renderer/FCMNotificationReceiver.js +++ b/src/js/ipc/renderer/FCMNotificationReceiver.js @@ -51,16 +51,20 @@ class FCMNotificationReceiver extends EventEmitter { } /** - * Ask the main process to register a new android device - * to receive fcm notifications for the provided senderId. + * Ask the main process to register a new android device to receive fcm notifications. * * Events Emitted: * - register.success * - register.error */ - register(senderId) { + register() { ipcRenderer.send('push-receiver.register', { - senderId: senderId, + apiKey: "AIzaSyB5y2y-Tzqb4-I4Qnlsh_9naYv_TD8pCvY", + projectId: "rust-companion-app", + gcmSenderId: "976529667804", + gmsAppId: "1:976529667804:android:d6f1ddeb4403b338fea619", + androidPackageName: "com.facepunch.rust.companion", + androidPackageCert: "E28D05345FB78A7A1A63D70F4A302DBF426CA5AD", }); } diff --git a/vue.config.js b/vue.config.js index eeb65766..70d096b9 100644 --- a/vue.config.js +++ b/vue.config.js @@ -15,6 +15,7 @@ module.exports = { }, externals: [ 'push-receiver', + '@liamcottle/push-receiver', ], }, },