11package ly .count .android .sdk .react ;
22
3+ import android .app .Application ;
4+ import android .content .Context ;
35import android .content .Intent ;
46import android .util .Log ;
57
@@ -25,19 +27,27 @@ public void onNewToken(String token) {
2527 @ Override
2628 public void onMessageReceived (RemoteMessage remoteMessage ) {
2729 super .onMessageReceived (remoteMessage );
30+ if (Countly .sharedInstance ().isLoggingEnabled ()) {
31+ Log .d (Countly .TAG , "[CountlyMessagingService] got new message: " + remoteMessage .getData ().toString ());
32+ }
33+ CountlyReactNative .onNotification (remoteMessage .getData ());
34+
2835
2936 if (!Countly .sharedInstance ().isInitialized ()) {
3037 int mode = CountlyPush .getLastMessagingMethod (this );
31- if (mode == 0 ) {
32- CountlyPush .init (getApplication (), Countly .CountlyMessagingMode .TEST );
33- } else if (mode == 1 ) {
34- CountlyPush .init (getApplication (), Countly .CountlyMessagingMode .PRODUCTION );
38+ Application application = getApplication ();
39+ if (application == null ){
40+ Log .d (Countly .TAG , "[CountlyMessagingService] getApplication() returns null: application must be non-null to init CountlyPush" );
41+ }
42+ else {
43+ if (mode == 0 ) {
44+ CountlyPush .init (application , Countly .CountlyMessagingMode .TEST );
45+ } else if (mode == 1 ) {
46+ CountlyPush .init (application , Countly .CountlyMessagingMode .PRODUCTION );
47+ }
3548 }
3649 }
3750
38- if (Countly .sharedInstance ().isLoggingEnabled ()) {
39- Log .d (Countly .TAG , "[CountlyMessagingService] got new message: " + remoteMessage .getData ().toString ());
40- }
4151
4252 // decode message data and extract meaningful information from it: title, body, badge, etc.
4353 CountlyPush .Message message = CountlyPush .decodeMessage (remoteMessage .getData ());
@@ -47,8 +57,12 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
4757 // message.recordAction(getApplicationContext());
4858 // return;
4959 // }
50-
51- Boolean result = CountlyPush .displayNotification (getApplicationContext (), message , getApplicationContext ().getApplicationInfo ().icon , null );
60+ Context context = getApplicationContext ();
61+ if (context == null ){
62+ Log .d (Countly .TAG , "[CountlyMessagingService] getApplicationContext() returns null: context must be non-null to displayNotification" );
63+ return ;
64+ }
65+ Boolean result = CountlyPush .displayNotification (context , message , context .getApplicationInfo ().icon , null );
5266 if (Countly .sharedInstance ().isLoggingEnabled ()) {
5367 if (result == null ) {
5468 Log .i (Countly .TAG , "[CountlyMessagingService] Message wasn't sent from Countly server, so it cannot be handled by Countly SDK" );
@@ -58,8 +72,6 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
5872 Log .i (Countly .TAG , "[CountlyMessagingService] Message wasn't handled by Countly SDK because API level is too low for Notification support or because currentActivity is null (not enough lifecycle method calls)" );
5973 }
6074 }
61-
62- CountlyReactNative .onNotification (remoteMessage .getData ());
6375 }
6476
6577 @ Override
0 commit comments