Skip to content

Instantly share code, notes, and snippets.

@ilight
Created April 18, 2024 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilight/be7141f45f15e39b6fccfe46ca6e3829 to your computer and use it in GitHub Desktop.
Save ilight/be7141f45f15e39b6fccfe46ca6e3829 to your computer and use it in GitHub Desktop.
Patch to wix's react-native-notifications 5.1.0
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;
public abstract class NotificationManagerCompatFacade {
public static NotificationManagerCompat from(@NonNull Context context) {
diff --git 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
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 5b7f15f..7b3ee7e 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
@@ -15,6 +15,7 @@ import com.wix.reactnativenotifications.core.AppLifecycleFacade;
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
import com.wix.reactnativenotifications.core.InitialNotificationHolder;
import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
+import com.wix.reactnativenotifications.core.ReactAppLifecycleFacade;
import com.wix.reactnativenotifications.core.notification.IPushNotification;
import com.wix.reactnativenotifications.core.notification.PushNotification;
import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer;
@@ -66,7 +67,12 @@ public class RNNotificationsPackage implements ReactPackage, AppLifecycleFacade.
@Override
public void onActivityStarted(Activity activity) {
- if (InitialNotificationHolder.getInstance().get() == null) {
+ boolean isReactInitialized = false;
+ if (AppLifecycleFacadeHolder.get() instanceof ReactAppLifecycleFacade) {
+ isReactInitialized = AppLifecycleFacadeHolder.get().isReactInitialized();
+ }
+
+ if (InitialNotificationHolder.getInstance().get() == null && !isReactInitialized) {
callOnOpenedIfNeed(activity);
}
}
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 1e7e871..62e5cb8 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/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java
index f9c858b..94ea188 100644
--- a/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java
+++ b/node_modules/react-native-notifications/lib/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;
public abstract class NotificationManagerCompatFacade {
public static NotificationManagerCompat from(@NonNull Context context) {
@ilight
Copy link
Author

ilight commented Apr 18, 2024

This patch was inspired from a 4.3.3 patch available online. When I upgraded to react-native-notifications v5.1.0, I deleted the 4.3.3 patch. The issue I faced without this patch was

  • To achieve Android Foreground Notification, I was using postLocalNotification(). This works fine but when I click on the notification (shown in system tray), I expect it to navigate to a certain screen. Instead, it was restarting the entire App (Activity) as if the App was freshly launched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment