1- const { register, listen} = require ( 'push-receiver' ) ;
1+ const {
2+ AndroidFCM,
3+ Client : PushReceiverClient ,
4+ } = require ( '@liamcottle/push-receiver' ) ;
25
36/**
47 * This class is responsible for registering a new android device with fcm
@@ -42,11 +45,8 @@ class FCMNotificationManager {
4245 event . sender . send ( 'push-receiver.notifications.listen.stopped' ) ;
4346 }
4447
45- onNotificationReceived ( event , notification , persistentId ) {
46- event . sender . send ( 'push-receiver.notifications.received' , {
47- 'notification' : notification ,
48- 'persistentId' : persistentId ,
49- } ) ;
48+ onNotificationReceived ( event , data ) {
49+ event . sender . send ( 'push-receiver.notifications.received' , data ) ;
5050 }
5151
5252 onNotificationError ( event , error ) {
@@ -64,7 +64,7 @@ class FCMNotificationManager {
6464 try {
6565
6666 // register with gcm/fcm
67- const credentials = await register ( data . senderId ) ;
67+ const credentials = await AndroidFCM . register ( data . apiKey , data . projectId , data . gcmSenderId , data . gmsAppId , data . androidPackageName , data . androidPackageCert ) ;
6868
6969 // registering was successful
7070 this . onRegisterSuccess ( event , credentials ) ;
@@ -90,13 +90,18 @@ class FCMNotificationManager {
9090 let persistentIds = data . persistentIds || [ ] ;
9191
9292 // start listening for notifications
93- this . notificationClient = await listen ( { ...credentials , persistentIds} , ( { notification, persistentId} ) => {
93+ const androidId = credentials . gcm . androidId ;
94+ const securityToken = credentials . gcm . securityToken ;
95+ const client = new PushReceiverClient ( androidId , securityToken , persistentIds ) ;
96+ client . on ( 'ON_DATA_RECEIVED' , ( data ) => {
9497
9598 // notification was received
96- this . onNotificationReceived ( event , notification , persistentId ) ;
99+ this . onNotificationReceived ( event , data ) ;
97100
98101 } ) ;
99102
103+ client . connect ( ) ;
104+
100105 // listening for notifications
101106 this . onNotificationListenStart ( event ) ;
102107
0 commit comments