1
1
import Token from "../Models/Token" ;
2
2
import fetch from "node-fetch" ;
3
+ import { google } from "googleapis" ;
4
+ import key from "../placeholders/firebase.json" ;
3
5
6
+ const PROJECT_ID = "<YOUR-PROJECT-ID>" ;
7
+ const HOST = "fcm.googleapis.com" ;
8
+ const PATH = "/v1/projects/" + PROJECT_ID + "/messages:send" ;
9
+ const MESSAGING_SCOPE = "https://www.googleapis.com/auth/firebase.messaging" ;
10
+ const SCOPES = [ MESSAGING_SCOPE ] ;
4
11
export default class Checker {
5
12
// List or all the crypto symbols that need to be checked
6
13
// private _symbols: Array<string>;
@@ -15,10 +22,10 @@ export default class Checker {
15
22
// public set tokens (v : Array<Token>) {
16
23
// this._tokens = v;
17
24
// }
18
-
19
25
constructor ( ) {
20
26
this . _interval = setInterval ( ( ) => { } ) ;
21
27
clearInterval ( this . _interval ) ;
28
+
22
29
// All the crypto symbols that need to be checked
23
30
this . _tokens = [
24
31
{
@@ -147,15 +154,16 @@ export default class Checker {
147
154
clearInterval ( this . _interval ) ;
148
155
}
149
156
public SendNotification ( title : string , msg : string ) {
150
- fetch (
151
- `https://fcm.googleapis.com/v1/projects/cryptobob-eaff8/messages:send` ,
152
- {
153
- method : "POST" ,
154
- headers : {
155
- Authorization :
156
- "Bearer ya29.a0ARrdaM9zZcAUyb0bT2EDZJ1YUNDB4RyJGmlyN1RvsBg-WZbj0xgadGed4mBqI3PtHzkn34pb7endIAku7Nikf4pqhWGDaAUKu7HeBiwt6una1fi2_IwmNVkWpjB3KuL18oJ9vLWmHCGnq5-OLIwbNI9Pr77k" ,
157
- } ,
158
- body : `
157
+ this . GetToken ( ) . then ( async ( accessToken ) => {
158
+ console . log ( accessToken ) ;
159
+ fetch (
160
+ `https://fcm.googleapis.com/v1/projects/cryptobob-eaff8/messages:send` ,
161
+ {
162
+ method : "POST" ,
163
+ headers : {
164
+ Authorization : `Bearer ${ accessToken } ` ,
165
+ } ,
166
+ body : `
159
167
{
160
168
"message": {
161
169
"notification": {
@@ -167,9 +175,33 @@ export default class Checker {
167
175
}
168
176
}
169
177
` ,
170
- }
171
- ) . then ( async ( res ) => {
172
- console . log ( res . status ) ;
178
+ }
179
+ ) . then ( async ( res ) => {
180
+ console . log ( res . status ) ;
181
+ } ) ;
182
+ } ) ;
183
+ }
184
+
185
+ private GetToken ( ) {
186
+ return new Promise ( function ( resolve , reject ) {
187
+ ///const key = require(".../placeholders/firebase.json");
188
+ const jwtClient = new google . auth . JWT (
189
+ key . client_email ,
190
+ "" ,
191
+ key . private_key ,
192
+ SCOPES ,
193
+ ""
194
+ ) ;
195
+ jwtClient . authorize ( async ( err , tokens ) => {
196
+ if ( tokens ) {
197
+ if ( err ) {
198
+ reject ( err ) ;
199
+ return ;
200
+ }
201
+
202
+ resolve ( tokens . access_token ) ;
203
+ }
204
+ } ) ;
173
205
} ) ;
174
206
}
175
207
}
0 commit comments