skip to Main Content

I have a React Native app with RN version "react-native": "0.72.7", and use react-native-notification with version ^5.1.0 for my push notification service for both Android and iOS applications.

The push notification service works fine in iOS without any issues. The push notification service for the Android app has some problems.

I am dealing with a reloading issue for the Android versions 11 and above. Following is the scenario for the bug.

Steps

  1. Open the Android app and keep it in the background
  2. Receive notification for the app and click on the notification from the notification center
  3. The app will open but the entire application gets reloaded

Accepted Criteria

  • The app should not reload when clicking on the push notifications

Can anybody give me some sense into why this is happening?

2

Answers


  1. Chosen as BEST ANSWER

    As per the research I have done and the resolutions I went through, I managed to find a working solution for the issue in a patch for the latest react-native-notification.

    You can find the patch here. Install the patch and run the app again with your credentials it will work.

    diff --git a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java
    index f9c858b..94ea188 100644
    --- a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java
    +++ b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java
    @@ -2,8 +2,8 @@
     package com.wix.reactnativenotifications;
     
     import android.content.Context;
    -import android.support.annotation.NonNull;
    -import android.support.v4.app.NotificationManagerCompat;
    +import androidx.annotation.NonNull;
    +import androidx.core.app.NotificationManagerCompat;
    

  2. It is hard to suggest a solution without a code or some log messages.
    Probably if you use adb logcat you could see some useful log messages that would help with this issue. But, I managed to find something related to your issue here.

    First, try updating your targetSdkVersion to 33, and see if it solves your problem. You can find it on your android/app/build.gradle file, for example:

    [...]
    buildscript {
        ext {
            buildToolsVersion = "30.0.2"
            minSdkVersion = 21
            compileSdkVersion = 30
            targetSdkVersion = 33 <--- here
            ndkVersion = "20.1.5948944"
        }
    }
    [...]
    

    After this, re-compile your app and test it.

    If it doesn’t help, you should try to make changes on the following files ( means you will remove it, + means you will add it):

    1. /node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java:
    //[...]     
         @Override
         public void onNewIntent(Intent intent) {
             if (NotificationIntentAdapter.canHandleIntent(intent)) {
    -            Bundle notificationData = intent.getExtras();
    +            Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
                 final IPushNotification notification = PushNotification.get(getReactApplicationContext().getApplicationContext(), notificationData);
                 if (notification != null) {
                     notification.onOpened();
    //[...]                      
    
    1. /node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java
    //[...]
             Intent intent = activity.getIntent();
             if (NotificationIntentAdapter.canHandleIntent(intent)) {
                 Context appContext = mApplication.getApplicationContext();
    -              Bundle notificationData = NotificationIntentAdapter.cannotHandleTrampolineActivity(appContext) ?
    -                    NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent) : intent.getExtras();
    +            Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
                 final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
                 if (pushNotification != null) {
                     pushNotification.onOpened();
    //[...]
    
    1. /node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java
    //[...]
         @SuppressLint("UnspecifiedImmutableFlag")
         public static PendingIntent createPendingNotificationIntent(Context appContext, PushNotificationProps notification) {
    -        if (cannotHandleTrampolineActivity(appContext)) {
    -            Intent mainActivityIntent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
    -            mainActivityIntent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
    -            TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(appContext);
    -            taskStackBuilder.addNextIntentWithParentStack(mainActivityIntent);
    -            return taskStackBuilder.getPendingIntent((int) System.currentTimeMillis(), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
    -        } else {
    -            Intent intent = new Intent(appContext, ProxyService.class);
    -            intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
    -            return PendingIntent.getService(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
    -        }
    +        Intent intent = appContext.getPackageManager().getLaunchIntentForPackage(appContext.getPackageName());
    +        intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
    +        return PendingIntent.getActivity(appContext, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
         }
    
         public static boolean cannotHandleTrampolineActivity(Context appContext) {
    //[...]     
    

    After these changes, re-compile your app and test it.

    These changes are not permanent, so to make them permanent you will need:

    1. Fork the react-native-notifications module on your GitHub account
    2. Make these changes on your fork.
    3. Uninstall the original react-native-notifications module by running: npm uninstall react-native-notifications
    4. install your fork by running: npm install git+https://github.com/yourUsername/YourForkNameHere

    Hope it helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search