@@ -66,7 +66,7 @@ public String toString(){
6666
6767public class CountlyReactNative extends ReactContextBaseJavaModule {
6868
69- public static final String TAG = "CountlyReactNative " ;
69+ public static final String TAG = "CountlyReactNativePlugin " ;
7070 private String COUNTLY_RN_SDK_VERSION_STRING = "20.04.7" ;
7171 private String COUNTLY_RN_SDK_NAME = "js-rnb-android" ;
7272
@@ -119,8 +119,13 @@ public void init(ReadableArray args, Promise promise){
119119 Countly .sharedInstance ().COUNTLY_SDK_VERSION_STRING = COUNTLY_RN_SDK_VERSION_STRING ;
120120
121121 this .config .setContext (_reactContext );
122- Activity activity = getCurrentActivity ();
123- this .config .setApplication (activity .getApplication ());
122+ Activity activity = getActivity ();
123+ if (activity != null ) {
124+ this .config .setApplication (activity .getApplication ());
125+ }
126+ else {
127+ log ("init, Activity is null, some features will not work" , LogLevel .WARNING );
128+ }
124129 if (deviceId == null || "" .equals (deviceId )){
125130 }else {
126131 if (deviceId .equals ("TemporaryDeviceID" )){
@@ -504,7 +509,11 @@ public void callback(String result) {
504509
505510 @ ReactMethod
506511 public void askForNotificationPermission (ReadableArray args ){
507- Activity activity = this ._reactContext .getCurrentActivity ();
512+ Activity activity = this .getActivity ();
513+ if (activity == null ) {
514+ log ("askForNotificationPermission failed, Activity is null" , LogLevel .ERROR );
515+ return ;
516+ }
508517 Context context = this ._reactContext ;
509518 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
510519 NotificationManager notificationManager = (NotificationManager ) context .getSystemService (context .NOTIFICATION_SERVICE );
@@ -532,7 +541,12 @@ public void onComplete(Task<InstanceIdResult> task) {
532541
533542 @ ReactMethod
534543 public void start (){
535- Countly .sharedInstance ().onStart (getCurrentActivity ());
544+ Activity activity = this .getActivity ();
545+ if (activity == null ) {
546+ log ("start failed, Activity is null" , LogLevel .ERROR );
547+ return ;
548+ }
549+ Countly .sharedInstance ().onStart (activity );
536550 }
537551
538552 @ ReactMethod
@@ -778,7 +792,11 @@ public void remoteConfigClearValues(Promise promise){
778792
779793 @ ReactMethod
780794 public void showStarRating (ReadableArray args , final Callback callback ){
781- Activity activity = getCurrentActivity ();
795+ Activity activity = getActivity ();
796+ if (activity == null ) {
797+ log ("showStarRating failed, Activity is null" , LogLevel .ERROR );
798+ return ;
799+ }
782800 Countly .sharedInstance ().ratings ().showStarRating (activity , new StarRatingCallback (){
783801
784802 @ Override
@@ -796,9 +814,13 @@ public void onDismiss() {
796814
797815 @ ReactMethod
798816 public void showFeedbackPopup (ReadableArray args ){
817+ Activity activity = getActivity ();
818+ if (activity == null ) {
819+ log ("showFeedbackPopup failed, Activity is null" , LogLevel .ERROR );
820+ return ;
821+ }
799822 String widgetId = args .getString (0 );
800823 String closeFeedBackButton = args .getString (1 );
801- Activity activity = getCurrentActivity ();
802824 Countly .sharedInstance ().ratings ().showFeedbackPopup ( widgetId , closeFeedBackButton , activity , null );
803825 }
804826
@@ -864,6 +886,12 @@ public void enableApm(ReadableArray args){
864886 public void enableAttribution (){
865887 this .config .setEnableAttribution (true );
866888 }
889+
890+ @ ReactMethod
891+ public void recordAttributionID (ReadableArray args ){
892+ String attributionID = args .getString (0 );
893+ log ("recordAttributionID: Not implemented for Android" );
894+ }
867895
868896 enum LogLevel {INFO , DEBUG , VERBOSE , WARNING , ERROR }
869897 static void log (String message , LogLevel logLevel ) {
@@ -893,4 +921,13 @@ static void log(String message, Throwable tr, LogLevel logLevel) {
893921 }
894922 }
895923
924+ Activity getActivity () {
925+ Activity activity = getCurrentActivity ();
926+ if (activity == null && _reactContext != null )
927+ {
928+ activity = _reactContext .getCurrentActivity ();
929+ }
930+ return activity ;
931+ }
932+
896933}
0 commit comments