3
3
const axios = require ( 'axios' ) ;
4
4
const express = require ( 'express' ) ;
5
5
const { v4 : uuidv4 } = require ( 'uuid' ) ;
6
- const { register, listen } = require ( 'push-receiver' ) ;
7
6
const commandLineArgs = require ( 'command-line-args' ) ;
8
7
const commandLineUsage = require ( 'command-line-usage' ) ;
9
8
const ChromeLauncher = require ( 'chrome-launcher' ) ;
10
9
const path = require ( 'path' ) ;
11
10
const fs = require ( 'fs' ) ;
11
+ const AndroidFCM = require ( '@liamcottle/push-receiver/src/android/fcm' ) ;
12
+ const PushReceiverClient = require ( "@liamcottle/push-receiver/src/client" ) ;
12
13
13
14
let server ;
14
15
let fcmClient ;
@@ -51,24 +52,23 @@ function updateConfig(configFile, config) {
51
52
fs . writeFileSync ( configFile , json , "utf8" ) ;
52
53
}
53
54
54
- async function getExpoPushToken ( credentials ) {
55
+ async function getExpoPushToken ( fcmToken ) {
55
56
const response = await axios . post ( 'https://exp.host/--/api/v2/push/getExpoPushToken' , {
57
+ type : 'fcm' ,
56
58
deviceId : uuidv4 ( ) ,
57
- experienceId : '@facepunch/RustCompanion' ,
59
+ development : false ,
58
60
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" ,
62
63
} ) ;
63
-
64
64
return response . data . data . expoPushToken ;
65
65
}
66
66
67
67
async function registerWithRustPlus ( authToken , expoPushToken ) {
68
68
return axios . post ( 'https://companion-rust.facepunch.com:443/api/push/register' , {
69
69
AuthToken : authToken ,
70
70
DeviceId : 'rustplus.js' ,
71
- PushKind : 0 ,
71
+ PushKind : 3 ,
72
72
PushToken : expoPushToken ,
73
73
} )
74
74
}
@@ -142,10 +142,16 @@ var rustplusAuthToken = null;
142
142
143
143
async function fcmRegister ( options ) {
144
144
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 ) ;
146
152
147
153
console . log ( "Fetching Expo Push Token" ) ;
148
- expoPushToken = await getExpoPushToken ( fcmCredentials ) . catch ( ( error ) => {
154
+ expoPushToken = await getExpoPushToken ( fcmCredentials . fcm . token ) . catch ( ( error ) => {
149
155
console . log ( "Failed to fetch Expo Push Token" ) ;
150
156
console . log ( error ) ;
151
157
process . exit ( 1 ) ;
@@ -194,9 +200,10 @@ async function fcmListen(options) {
194
200
}
195
201
196
202
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 ) => {
200
207
201
208
// generate timestamp
202
209
const timestamp = new Date ( ) . toLocaleString ( ) ;
@@ -205,9 +212,17 @@ async function fcmListen(options) {
205
212
console . log ( '\x1b[32m%s\x1b[0m' , `[${ timestamp } ] Notification Received` )
206
213
207
214
// log notification body
208
- console . log ( body ) ;
215
+ console . log ( data ) ;
209
216
210
217
} ) ;
218
+
219
+ // force exit on ctrl + c
220
+ process . on ( 'SIGINT' , async ( ) => {
221
+ process . exit ( 0 ) ;
222
+ } ) ;
223
+
224
+ await client . connect ( ) ;
225
+
211
226
}
212
227
213
228
function showUsage ( ) {
0 commit comments