Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 11 and above crashing on notification open #835

Closed
MajkellVZ opened this issue Mar 8, 2022 · 34 comments · Fixed by #862
Closed

Android 11 and above crashing on notification open #835

MajkellVZ opened this issue Mar 8, 2022 · 34 comments · Fixed by #862
Assignees
Labels

Comments

@MajkellVZ
Copy link

MajkellVZ commented Mar 8, 2022

The app crashes while opening the notification and shows the following message
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.clone()' on a null object reference

currently using version 4.2.3

@StefanLuecke
Copy link

same here. Anyone already fixed this issue ?

@DanielEliraz
Copy link
Collaborator

@StefanLuecke , @MajkellVZ , please see if it's fixed in 4.2.4

@DanielEliraz DanielEliraz self-assigned this Mar 9, 2022
@MajkellVZ
Copy link
Author

@DanielEliraz the error persists

@StefanLuecke
Copy link

@DanielEliraz Seems to work for me ... will test it on multiple devices tomorrow. Thx for your awesome work!

@MajkellVZ
Copy link
Author

for me it works when the app is killed but not when it's in background, but so did with the last version

@MajkellVZ
Copy link
Author

lowered the targetSkdVersion to 30 and it worked

@DanielEliraz
Copy link
Collaborator

@MajkellVZ Need to find a way to solve it also in targetSkdVersion > 30. In my app I added the logic in the MainActivity but it seems hucky. But for now ReactNative's targetSkdVersion is 30 so there is time to think

@ali-io
Copy link

ali-io commented Mar 11, 2022

@MajkellVZ Need to find a way to solve it also in targetSkdVersion > 30. In my app I added the logic in the MainActivity but it seems hucky. But for now ReactNative's targetSkdVersion is 30 so there is time to think

can you please share your solution without the need to lower the sdk to 30?

@taranda
Copy link

taranda commented Mar 22, 2022

@DanielEliraz This problem persists for me in 4.2.4 with Android 12 and target SDK 31. I recommend keeping the issue open until this package works with > 30.

@DanielEliraz DanielEliraz reopened this Mar 23, 2022
@pepf
Copy link

pepf commented Mar 23, 2022

Same for our app running on Android 12 devices with targetSDKVersion set to 31 (considering turning it back to 30 now 😅 )
Versions:

   "react-native-notifications": "^4.2.4",
   "react-native": "0.67.2",

Sample stack trace. From the 100+ reports I can see it only happens at getInitialNotification calls from "cold boots", if the app is already running it seems to open fine:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.clone()' on a null object reference
    at com.wix.reactnativenotifications.core.notification.PushNotificationProps.asBundle(PushNotificationProps.java:26)
    at com.wix.reactnativenotifications.RNNotificationsModule.getInitialNotification(RNNotificationsModule.java:92)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
    at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188)
    at com.facebook.react.bridge.queue.NativeRunnable.run(NativeRunnable.java)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
    at java.lang.Thread.run(Thread.java:920)

@taranda
Copy link

taranda commented Mar 23, 2022

The only solution I found was to roll back to SDK 30.

@stale
Copy link

stale bot commented Apr 23, 2022

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

@DanielEliraz
Copy link
Collaborator

@taranda @pepf , can you please try it with version 4.3.1?

@pepf
Copy link

pepf commented Apr 24, 2022

Checked latest version on an android (12) emulator with targetSdkVersion = 31 set the app build.
No crash! 🙏

Upon inspecting the adb logs, opening a push notification as a cold boot (no activity) triggers the exception:
Screenshot 2022-04-24 at 19 57 27

Opening a push notification when the activity is already running this exception is not triggered.

@junebugfix
Copy link

Hmm, @DanielEliraz I'm still experiencing this on 4.3.1 when opening a push notification as a cold boot with targetSdkVersion = 31 and Android API level both 30 and 32. react-native v0.68.0

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.clone()' on a null object reference
        at com.wix.reactnativenotifications.core.notification.PushNotificationProps.asBundle(PushNotificationProps.java:26)
        at com.wix.reactnativenotifications.core.notification.PushNotification.notifyOpenedToJS(PushNotification.java:210)
        at com.wix.reactnativenotifications.core.notification.PushNotification.dispatchImmediately(PushNotification.java:127)
        at com.wix.reactnativenotifications.core.notification.PushNotification$1.onAppVisible(PushNotification.java:36)
        at com.wix.reactnativenotifications.core.ReactAppLifecycleFacade.switchToVisible(ReactAppLifecycleFacade.java:88)
        at com.wix.reactnativenotifications.core.ReactAppLifecycleFacade.access$000(ReactAppLifecycleFacade.java:14)
        at com.wix.reactnativenotifications.core.ReactAppLifecycleFacade$1.onHostResume(ReactAppLifecycleFacade.java:27)
        at com.facebook.react.bridge.ReactContext.onHostResume(ReactContext.java:263)
        at com.facebook.react.ReactInstanceManager.moveToResumedLifecycleState(ReactInstanceManager.java:784)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:682)
        at com.facebook.react.ReactInstanceManager.onHostResume(ReactInstanceManager.java:632)
        at com.facebook.react.ReactDelegate.onHostResume(ReactDelegate.java:53)
        at com.facebook.react.ReactActivityDelegate.onResume(ReactActivityDelegate.java:100)
        at com.facebook.react.ReactActivity.onResume(ReactActivity.java:58)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
        at android.app.Activity.performResume(Activity.java:8129)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4434)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4476)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

@jonesaustindev
Copy link

jonesaustindev commented Apr 28, 2022

@DanielEliraz So far having the same results as @hankhester with the new 4.3.1 version

@pepf
Copy link

pepf commented Jun 9, 2022

After some monitoring I have to retract my earlier statement that this was fixed, I encounter crashes with the similar stack trace as @hankhester reported here.

So, @DanielEliraz just to prevent these unhandled crashes, would it be possible to patch the`notifiyOpenedToJS method to include a try...catch statement? From:

private void notifyOpenedToJS() {
Bundle response = new Bundle();
response.putBundle("notification", mNotificationProps.asBundle());
mJsIOHelper.sendEventToJS(NOTIFICATION_OPENED_EVENT_NAME, response, mAppLifecycleFacade.getRunningReactContext());
}

to

    private void notifyOpenedToJS() {
        Bundle response = new Bundle();
        try {
            response.putBundle("notification", mNotificationProps.asBundle());
        } catch (NullPointerException e) {
            Log.e(LOGTAG, "notifyOpenedToJS: Null pointer exception");
        }

Just like you did in #835?

@pepf
Copy link

pepf commented Jun 10, 2022

Using patch-package you can apply this patch.. it seems to have worked for me:
https://gist.github.com/pepf/742d4772545400919aece5649402b9e7

Logs when reproducing:
Screenshot 2022-06-10 at 11 17 17

@artyorsh
Copy link

artyorsh commented Jul 6, 2022

@DanielEliraz the issue still exist in v4.3.1 unfortunately. We were that unlucky project who sent ~1k push notifications to users and got ~200 crash reports same day

Android 11 and above | RN 0.68.2 | Logs

@MajkellVZ
Copy link
Author

Why is this issue closed when the crash is still persisting?

@walterholohan
Copy link

Same here, still getting crashes with this error

@tkyr-hh
Copy link

tkyr-hh commented Sep 20, 2022

@DanielEliraz Kindly, can you re-open this issue as it is still happening to me on Android 11 and above.

@Vednus
Copy link

Vednus commented Sep 24, 2022

Okay, this took way too long, but I got things working with targetSdkVersion 31. I'm also on RN 68.2. There are a few places things could be going wrong for everyone.

First, you need to run this patch: #874 (comment)
You can also combine the patch in this thread (#835 (comment)) to handle any exceptions, but they shouldn't happen if you get everything working.

Next, you need to make sure your AndroidManifest.xml handles both deeplinks and universal links if you plan on using both. The structure was changed in Android 12 due to security stuff and now you need separate intents for each type of link (example:// and https://www.example.com). Here's an example from my app. The two schemes I use are deep links: https://www.pebbleclimbing.com and app links: pebble://

    <activity android:name=".MainActivity"
      android:label="@string/app_name"
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
      android:launchMode="singleTask"
      android:screenOrientation="portrait"
      android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
      android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "http://www.pebbleclimbing.com/” -->
        <data android:scheme="https"
              android:host="www.pebbleclimbing.com"
              android:pathPrefix="/" />
        <!-- note that the leading "/" is required for pathPrefix-->
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- Accepts URIs that begin with "pebble:// -->
        <data android:scheme="pebble" />
    </intent-filter>
    </activity>

Next, when setting stuff up, you need to create a channel on app startup. See here in the docs: https://wix.github.io/react-native-notifications/api/android-api#setnotificationchannel

Now, if you want to have foreground notifications, you need to duplicate the incoming notification as a local notification outlined here: #869 (comment) But, this doesn't work because we're going to be sending a channel id with our remote notifications as well (remote notifications won't open the app without it). So, instead of checking for lack of a channel id to see if you need to create a duplicated local notification, add some sort of boolean to the notification when duplicating it for the foreground notification. ex:

if (!notification?.payload?.is_local_foreground) {
            // notification.payload.android_channel_id = "pebble-channel-id"
            notification.payload.is_local_foreground = true
            let nId = Notifications.postLocalNotification(notification.payload)
            console.log(nId)
          }

Finally, when you send the notification from the server, you need to send the channel id with the data so that it will open your app. I'm using amazon pinpoint and my raw message data structure looks like the following, but yours might be different and need some alternate massaging. This structure is pretty important. If I added anything outside the data object, the notification would not open the app if it was in the background/killed :


{
  "GCMMessage": {
    "data": {
      "title": "Pebble test gym",
      "body": "This is the body of the message for Pebble test gym.",
      "url": "pebble://gyms/pebble-test",
      "android_channel_id": "pebble-channel-id"
    }
  }
}

Also, if your app is killed, you need to get the notification using getInitialNotification: https://wix.github.io/react-native-notifications/api/general-api/#getinitialnotification

Hopefully this helps y'all. This library does work for Android 13, but there are a lot of gotchas.

@brunoro
Copy link

brunoro commented Oct 26, 2022

@DanielEliraz we're still experiencing this on 4.3.2 (no patches)

@wavesandwaves
Copy link

Some members of my team are also experiencing this on 4.3.2, although it might be something device specific, as for others the bug has been fixed.

This patch seemed to work for those who are now having issues with 4.3.2:
#835 (comment)

@MajkellVZ
Copy link
Author

Tried the solution provided by @Vednus and it doesn't crash and I think that's mostly because of #835 (comment) but now when opening the notification it doesn't redirect to the desired screen.

@MajkellVZ
Copy link
Author

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 eadf41e..5903e02 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
@@ -17,7 +17,6 @@ import com.wix.reactnativenotifications.core.InitialNotificationHolder;
 import com.wix.reactnativenotifications.core.NotificationIntentAdapter;
 import com.wix.reactnativenotifications.core.notification.IPushNotification;
 import com.wix.reactnativenotifications.core.notification.PushNotification;
-import com.wix.reactnativenotifications.core.notification.PushNotificationProps;
 import com.wix.reactnativenotifications.core.notificationdrawer.IPushNotificationsDrawer;
 import com.wix.reactnativenotifications.core.notificationdrawer.PushNotificationsDrawer;
 
@@ -96,9 +95,7 @@ public class RNNotificationsPackage implements ReactPackage, AppLifecycleFacade.
         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);
+            final IPushNotification pushNotification = PushNotification.get(appContext, intent.getExtras());
             if (pushNotification != null) {
                 pushNotification.onOpened();
             }

this is enough to target SDK 31 without any crashes

@jamesholcomb
Copy link

Tried the solution provided by @Vednus and it doesn't crash and I think that's mostly because of #835 (comment) but now when opening the notification it doesn't redirect to the desired screen.

Also experience this when the app is in the background or killed state.

@sanchitos
Copy link

sanchitos commented Feb 9, 2023

RN 0.71 and Android 11 API 30 still has problems.
RNN 4.3.3

Close the app, receive a notification, open notification and crashes.

Applied the patch, however the notification is not being correctly handled, can't retrieve the information.

@kg-currenxie
Copy link

@MajkellVZ your patch worked for me!! <3
Android 11 and 13

@AlkanV
Copy link

AlkanV commented May 9, 2023

i have the same problem, any fix on this?

@levepic
Copy link

levepic commented Nov 15, 2023

Same crash

@mehranand1
Copy link

same problem

@stydeveloper
Copy link

still same issue appear in 4.3.3 and RN 0.73 and android version 11(30) and above !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.