wix / react-native-notifications

React Native Notifications
MIT License
3.23k stars 763 forks source link

Notification opened when app is opened from app tray (Android only) #872

Closed JadEl-Houssami closed 2 years ago

JadEl-Houssami commented 2 years ago

react-native-notifications version 4.2.4 on React Native 0.64.1. Also tried on react-native-notifications version 4.3.1 on React Native 0.67.

Steps

Expected Result onNotificationOpened() event listener is not fired and the first screen of the app is shown

Actual Result onNotificationOpened() event listener is fired and step 2 happens.

If I comment out the call to callOnOpenIfNeed() in onActivityStarted() in RNNotificationsPackage.java, this only happens the first time after the app is hard closed. I believe that this function is causing the issue to happen and notification.onOpened() is being called incorrectly as the Intent.getExtras() should not have the notification object.

See below for reference:

    private void callOnOpenedIfNeed(Activity activity) {
        Intent intent = activity.getIntent();
        if (NotificationIntentAdapter.canHandleIntent(intent)) {
            Context appContext = mApplication.getApplicationContext();
            Bundle notificationData = NotificationIntentAdapter.cannotHandleTrampolineActivity(appContext) ?
                    NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent) : intent.getExtras();
            final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
            if (pushNotification != null) {
                pushNotification.onOpened();
            }
        }
    }

I have seen this on Android versions 9-12.

nates-dennis commented 2 years ago

This is also an issue for me too. It seems to be based around the InitialNotificationholder not clearing the notification data after launching from a cold start.

Device Android 12 and Android 9 running 4.2.4 with react Native 0.64

ally03 commented 2 years ago

I am also facing this issue.

RatFou commented 2 years ago

Same issue here. As @nates-dennis said, it seems to be a InitialNotificationHolder error

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] commented 2 years ago

The issue has been closed for inactivity.

anisimov74 commented 1 year ago

After debugging I found out, that the method callOnOpenedIfNeed(activity) shouldn't be invoked. And I see no reasons why it should be. Maybe I missing something?

I patched the module, but ready to prepare a PR if needed.

notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java

   @Override
   public void onActivityStarted(Activity activity) {
      if (InitialNotificationHolder.getInstance().get() == null) {
          callOnOpenedIfNeed(activity); // <==
      }
   }
cw-sanjeev commented 1 year ago

@anisimov74 Can you share your fix here? Thanks

anisimov74 commented 1 year ago

@cw-sanjeev I use the following patch:

index 90969b2..4c00e69 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsModule.java
@@ -63,7 +63,7 @@ public class RNNotificationsModule extends ReactContextBaseJavaModule implements
     @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();
diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java
index a249c6a..5bb21be 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/RNNotificationsPackage.java
@@ -66,9 +66,6 @@ public class RNNotificationsPackage implements ReactPackage, AppLifecycleFacade.

     @Override
     public void onActivityStarted(Activity activity) {
-        if (InitialNotificationHolder.getInstance().get() == null) {
-            callOnOpenedIfNeed(activity);
-        }
     }

     @Override
@@ -95,8 +92,7 @@ public class RNNotificationsPackage implements ReactPackage, AppLifecycleFacade.
         Intent intent = activity.getIntent();
         if (NotificationIntentAdapter.canHandleIntent(intent)) {
             Context appContext = mApplication.getApplicationContext();
-            Bundle notificationData = NotificationIntentAdapter.canHandleTrampolineActivity(appContext) ?
-                    intent.getExtras() : NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
+            Bundle notificationData = NotificationIntentAdapter.extractPendingNotificationDataFromIntent(intent);
             final IPushNotification pushNotification = PushNotification.get(appContext, notificationData);
             if (pushNotification != null) {
                 pushNotification.onOpened();
diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java
index 70f609a..4484808 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java
+++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/NotificationIntentAdapter.java
@@ -14,17 +14,9 @@ public class NotificationIntentAdapter {

     @SuppressLint("UnspecifiedImmutableFlag")
     public static PendingIntent createPendingNotificationIntent(Context appContext, PushNotificationProps notification) {
-        if (canHandleTrampolineActivity(appContext)) {
-            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);
-        } else {
-            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);
-        }
+        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 canHandleTrampolineActivity(Context appContext) {
diff --git a/node_modules/react-native-notifications/react-native.config.js b/node_modules/react-native-notifications/react-native.config.js
index 437783b..41a91c4 100644
--- a/node_modules/react-native-notifications/react-native.config.js
+++ b/node_modules/react-native-notifications/react-native.config.js
@@ -6,8 +6,7 @@ module.exports = {
         sourceDir: './lib/android/app',
         packageInstance: 'new RNNotificationsPackage(reactNativeHost.getApplication())',
       }
-    },
-    assets: []
+    }
   },
   project: {
     ios: {