Skip to content

Commit 268b080

Browse files
committed
use custom push-receiver lib to support receiving android push notifications
1 parent 96a3cac commit 268b080

File tree

3 files changed

+587
-394
lines changed

3 files changed

+587
-394
lines changed

cli/index.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
const axios = require('axios');
44
const express = require('express');
55
const { v4: uuidv4 } = require('uuid');
6-
const { register, listen } = require('push-receiver');
76
const commandLineArgs = require('command-line-args');
87
const commandLineUsage = require('command-line-usage');
98
const ChromeLauncher = require('chrome-launcher');
109
const path = require('path');
1110
const fs = require('fs');
11+
const AndroidFCM = require('@liamcottle/push-receiver/src/android/fcm');
12+
const PushReceiverClient = require("@liamcottle/push-receiver/src/client");
1213

1314
let server;
1415
let fcmClient;
@@ -51,24 +52,23 @@ function updateConfig(configFile, config) {
5152
fs.writeFileSync(configFile, json, "utf8");
5253
}
5354

54-
async function getExpoPushToken(credentials) {
55+
async function getExpoPushToken(fcmToken) {
5556
const response = await axios.post('https://exp.host/--/api/v2/push/getExpoPushToken', {
57+
type: 'fcm',
5658
deviceId: uuidv4(),
57-
experienceId: '@facepunch/RustCompanion',
59+
development: false,
5860
appId: 'com.facepunch.rust.companion',
59-
deviceToken: credentials.fcm.token,
60-
type: 'fcm',
61-
development: false
61+
deviceToken: fcmToken,
62+
projectId: "49451aca-a822-41e6-ad59-955718d0ff9c",
6263
});
63-
6464
return response.data.data.expoPushToken;
6565
}
6666

6767
async function registerWithRustPlus(authToken, expoPushToken) {
6868
return axios.post('https://companion-rust.facepunch.com:443/api/push/register', {
6969
AuthToken: authToken,
7070
DeviceId: 'rustplus.js',
71-
PushKind: 0,
71+
PushKind: 3,
7272
PushToken: expoPushToken,
7373
})
7474
}
@@ -142,10 +142,16 @@ var rustplusAuthToken = null;
142142

143143
async function fcmRegister(options) {
144144
console.log("Registering with FCM");
145-
const fcmCredentials = await register('976529667804');
145+
const apiKey = "AIzaSyB5y2y-Tzqb4-I4Qnlsh_9naYv_TD8pCvY";
146+
const projectId = "rust-companion-app";
147+
const gcmSenderId = "976529667804";
148+
const gmsAppId = "1:976529667804:android:d6f1ddeb4403b338fea619";
149+
const androidPackageName = "com.facepunch.rust.companion";
150+
const androidPackageCert = "E28D05345FB78A7A1A63D70F4A302DBF426CA5AD";
151+
const fcmCredentials = await AndroidFCM.register(apiKey, projectId, gcmSenderId, gmsAppId, androidPackageName, androidPackageCert);
146152

147153
console.log("Fetching Expo Push Token");
148-
expoPushToken = await getExpoPushToken(fcmCredentials).catch((error) => {
154+
expoPushToken = await getExpoPushToken(fcmCredentials.fcm.token).catch((error) => {
149155
console.log("Failed to fetch Expo Push Token");
150156
console.log(error);
151157
process.exit(1);
@@ -194,9 +200,10 @@ async function fcmListen(options) {
194200
}
195201

196202
console.log("Listening for FCM Notifications");
197-
fcmClient = await listen(config.fcm_credentials, ({ notification, persistentId }) => {
198-
// parse notification body
199-
const body = JSON.parse(notification.data.body);
203+
const androidId = config.fcm_credentials.gcm.androidId;
204+
const securityToken = config.fcm_credentials.gcm.securityToken;
205+
const client = new PushReceiverClient(androidId, securityToken, []);
206+
client.on('ON_DATA_RECEIVED', (data) => {
200207

201208
// generate timestamp
202209
const timestamp = new Date().toLocaleString();
@@ -205,9 +212,17 @@ async function fcmListen(options) {
205212
console.log('\x1b[32m%s\x1b[0m', `[${timestamp}] Notification Received`)
206213

207214
// log notification body
208-
console.log(body);
215+
console.log(data);
209216

210217
});
218+
219+
// force exit on ctrl + c
220+
process.on('SIGINT', async () => {
221+
process.exit(0);
222+
});
223+
224+
await client.connect();
225+
211226
}
212227

213228
function showUsage() {

0 commit comments

Comments
 (0)