33const axios = require ( 'axios' ) ;
44const express = require ( 'express' ) ;
55const { v4 : uuidv4 } = require ( 'uuid' ) ;
6- const { register, listen } = require ( 'push-receiver' ) ;
76const commandLineArgs = require ( 'command-line-args' ) ;
87const commandLineUsage = require ( 'command-line-usage' ) ;
98const ChromeLauncher = require ( 'chrome-launcher' ) ;
109const path = require ( 'path' ) ;
1110const fs = require ( 'fs' ) ;
11+ const AndroidFCM = require ( '@liamcottle/push-receiver/src/android/fcm' ) ;
12+ const PushReceiverClient = require ( "@liamcottle/push-receiver/src/client" ) ;
1213
1314let server ;
1415let 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
6767async 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
143143async 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
213228function showUsage ( ) {
0 commit comments