Skip to content

Commit a792e59

Browse files
committed
migrate to new push receiver implementation for android fcm
1 parent 96ae0f5 commit a792e59

File tree

9 files changed

+106
-73
lines changed

9 files changed

+106
-73
lines changed

package-lock.json

Lines changed: 60 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"postuninstall": "electron-builder install-app-deps"
1515
},
1616
"dependencies": {
17+
"@liamcottle/push-receiver": "^0.0.4",
1718
"@tailwindcss/forms": "^0.2.1",
1819
"@tailwindcss/postcss7-compat": "^2.0.2",
1920
"autoprefixer": "^9",
@@ -25,7 +26,6 @@
2526
"long": "^4.0.0",
2627
"postcss": "^7",
2728
"protobufjs": "^6.10.2",
28-
"push-receiver": "^2.1.1",
2929
"tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.0.2",
3030
"uuid": "^8.3.2",
3131
"vue": "^2.6.11",

src/App.vue

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ export default {
282282
283283
// configure expo data
284284
var deviceId = window.DataStore.Config.getExpoDeviceId();
285-
var experienceId = '@facepunch/RustCompanion';
285+
var projectId = '49451aca-a822-41e6-ad59-955718d0ff9c';
286286
var appId = 'com.facepunch.rust.companion';
287287
var fcmToken = window.DataStore.FCM.getCredentials().fcm.token;
288288
289289
// register expo token
290290
this.expoStatus = Status.NOT_READY;
291291
this.expoStatusMessage = "Registering...";
292-
this.expoPushTokenReceiver.register(deviceId, experienceId, appId, fcmToken);
292+
this.expoPushTokenReceiver.register(deviceId, projectId, appId, fcmToken);
293293
294294
},
295295
@@ -303,29 +303,23 @@ export default {
303303
// save persistent id to data store
304304
window.DataStore.FCM.addPersistentId(data.persistentId);
305305
306-
// make sure notification exists
307-
var notification = data.notification;
308-
if(!notification){
309-
console.log("notification is null!");
306+
// make sure app data exists
307+
var appData = data.appData;
308+
if(!appData){
309+
console.log("FCM notification appData is null!");
310310
return;
311311
}
312312
313-
// make sure notification has data
314-
if(!notification.data){
315-
console.log("notification has no data!");
316-
console.log(notification);
313+
// make sure app data has body
314+
const body = appData.find((item) => item.key === "body");
315+
if(!body){
316+
console.log("FCM notification appData has no body!");
317+
console.log(appData);
317318
return;
318319
}
319320
320-
// make sure notification has body
321-
if(!notification.data.body){
322-
console.log("notification has no body!");
323-
console.log(notification);
324-
return;
325-
}
326-
327-
// parse notification
328-
var notificationBody = JSON.parse(notification.data.body);
321+
// parse body
322+
var notificationBody = JSON.parse(body.value);
329323
330324
// make sure body has type
331325
if(!notificationBody.type){
@@ -430,7 +424,7 @@ export default {
430424
431425
// register for a new set of fcm credentials
432426
this.fcmStatus = "Registering...";
433-
this.fcmNotificationReceiver.register('976529667804');
427+
this.fcmNotificationReceiver.register();
434428
435429
}
436430

src/js/ipc/main/ExpoPushTokenManager.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class ExpoPushTokenManager {
4040

4141
// register with expo
4242
axios.post('https://exp.host/--/api/v2/push/getExpoPushToken', {
43+
type: data.type,
4344
deviceId: data.deviceId,
44-
experienceId: data.experienceId,
45+
development: data.development,
4546
appId: data.appId,
4647
deviceToken: data.deviceToken,
47-
type: data.type,
48-
development: data.development,
48+
projectId: data.projectId,
4949
}, {
5050

5151
/**

src/js/ipc/main/FCMNotificationManager.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
const {register, listen} = require('push-receiver');
1+
const {
2+
AndroidFCM,
3+
Client: PushReceiverClient,
4+
} = require('@liamcottle/push-receiver');
25

36
/**
47
* This class is responsible for registering a new android device with fcm
@@ -42,11 +45,8 @@ class FCMNotificationManager {
4245
event.sender.send('push-receiver.notifications.listen.stopped');
4346
}
4447

45-
onNotificationReceived(event, notification, persistentId) {
46-
event.sender.send('push-receiver.notifications.received', {
47-
'notification': notification,
48-
'persistentId': persistentId,
49-
});
48+
onNotificationReceived(event, data) {
49+
event.sender.send('push-receiver.notifications.received', data);
5050
}
5151

5252
onNotificationError(event, error) {
@@ -64,7 +64,7 @@ class FCMNotificationManager {
6464
try {
6565

6666
// register with gcm/fcm
67-
const credentials = await register(data.senderId);
67+
const credentials = await AndroidFCM.register(data.apiKey, data.projectId, data.gcmSenderId, data.gmsAppId, data.androidPackageName, data.androidPackageCert);
6868

6969
// registering was successful
7070
this.onRegisterSuccess(event, credentials);
@@ -90,13 +90,18 @@ class FCMNotificationManager {
9090
let persistentIds = data.persistentIds || [];
9191

9292
// start listening for notifications
93-
this.notificationClient = await listen({...credentials, persistentIds}, ({notification, persistentId}) => {
93+
const androidId = credentials.gcm.androidId;
94+
const securityToken = credentials.gcm.securityToken;
95+
const client = new PushReceiverClient(androidId, securityToken, persistentIds);
96+
client.on('ON_DATA_RECEIVED', (data) => {
9497

9598
// notification was received
96-
this.onNotificationReceived(event, notification, persistentId);
99+
this.onNotificationReceived(event, data);
97100

98101
});
99102

103+
client.connect();
104+
100105
// listening for notifications
101106
this.onNotificationListenStart(event);
102107

src/js/ipc/main/RustCompanionManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class RustCompanionManager {
4040
axios.post('https://companion-rust.facepunch.com:443/api/push/register', {
4141
AuthToken: data.token,
4242
DeviceId: data.deviceId,
43-
PushKind: 0,
43+
PushKind: 3,
4444
PushToken: data.expoPushToken,
4545
}).then((response) => {
4646

src/js/ipc/renderer/ExpoPushTokenReceiver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ class ExpoPushTokenReceiver extends EventEmitter {
3737
* - register.success
3838
* - register.error
3939
*/
40-
register(deviceId, experienceId, appId, fcmToken) {
40+
register(deviceId, projectId, appId, fcmToken) {
4141
ipcRenderer.send('expo-push-token.register', {
42+
type: 'fcm',
4243
deviceId: deviceId,
43-
experienceId: experienceId,
44+
development: false,
4445
appId: appId,
4546
deviceToken: fcmToken,
46-
type: 'fcm',
47-
development: false,
47+
projectId: projectId,
4848
});
4949
}
5050

src/js/ipc/renderer/FCMNotificationReceiver.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,20 @@ class FCMNotificationReceiver extends EventEmitter {
5151
}
5252

5353
/**
54-
* Ask the main process to register a new android device
55-
* to receive fcm notifications for the provided senderId.
54+
* Ask the main process to register a new android device to receive fcm notifications.
5655
*
5756
* Events Emitted:
5857
* - register.success
5958
* - register.error
6059
*/
61-
register(senderId) {
60+
register() {
6261
ipcRenderer.send('push-receiver.register', {
63-
senderId: senderId,
62+
apiKey: "AIzaSyB5y2y-Tzqb4-I4Qnlsh_9naYv_TD8pCvY",
63+
projectId: "rust-companion-app",
64+
gcmSenderId: "976529667804",
65+
gmsAppId: "1:976529667804:android:d6f1ddeb4403b338fea619",
66+
androidPackageName: "com.facepunch.rust.companion",
67+
androidPackageCert: "E28D05345FB78A7A1A63D70F4A302DBF426CA5AD",
6468
});
6569
}
6670

vue.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
},
1616
externals: [
1717
'push-receiver',
18+
'@liamcottle/push-receiver',
1819
],
1920
},
2021
},

0 commit comments

Comments
 (0)