Skip to content

Commit

Permalink
migrate to new push receiver implementation for android fcm
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Sep 10, 2024
1 parent 96ae0f5 commit a792e59
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 73 deletions.
91 changes: 60 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
34 changes: 14 additions & 20 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand All @@ -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){
Expand Down Expand Up @@ -430,7 +424,7 @@ export default {
// register for a new set of fcm credentials
this.fcmStatus = "Registering...";
this.fcmNotificationReceiver.register('976529667804');
this.fcmNotificationReceiver.register();
}
Expand Down
6 changes: 3 additions & 3 deletions src/js/ipc/main/ExpoPushTokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}, {

/**
Expand Down
23 changes: 14 additions & 9 deletions src/js/ipc/main/FCMNotificationManager.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/js/ipc/main/RustCompanionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Expand Down
8 changes: 4 additions & 4 deletions src/js/ipc/renderer/ExpoPushTokenReceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
12 changes: 8 additions & 4 deletions src/js/ipc/renderer/FCMNotificationReceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
}

Expand Down
1 change: 1 addition & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
},
externals: [
'push-receiver',
'@liamcottle/push-receiver',
],
},
},
Expand Down

0 comments on commit a792e59

Please sign in to comment.