aws-sdk-android icon indicating copy to clipboard operation
aws-sdk-android copied to clipboard

Null Pointer Exception when Creating a Notification in Pinpoint

Open nzayatz14 opened this issue 2 years ago • 12 comments

Describe the bug

In my Crashes & ANR section of my app in the Google Play Store, I have noticed an uptick in crashes caused by AWS Pinpoint. I began to notice this uptick after updating my packages to version 2.47.0 and they still remain after updating to version 2.51.0. My current imports are as follows:

implementation 'com.amazonaws:aws-android-sdk-pinpoint:2.51.0'
implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.51.0'

The stacktrace is as follows:

Exception java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
  at com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClientBase.createNotification (NotificationClientBase.java:11)
  at com.amazonaws.mobileconnectors.pinpoint.targeting.notification.NotificationClientBase$1.run (NotificationClientBase.java:11)
  at java.lang.Thread.run (Thread.java:919)

Which AWS service(s) are affected?

Pinpoint SDK for Android

Environment Information (please complete the following information):

  • AWS Android SDK Version: 2.51.0
  • Device: Multiple
  • Android Version: 10, 11, and 12
  • Specific to simulators: No

nzayatz14 avatar Sep 08 '22 14:09 nzayatz14

Thanks for reaching out, @nzayatz14! It seems like this may happen when the library is unable to construct a notification channel -- you may want to look for Logcat messages involving that term. We will work on making this more bulletproof, but this may also indicate an issue with the app/devices that are crashing, for example if your app registers a separate notification channel with the same ID (PINPOINT.NOTIFICATION) or actively deletes notification channels.

tjleing avatar Sep 12 '22 22:09 tjleing

Hi @tjleing,

So I am actually creating my notification channels outside of Pinpoint. Does Pinpoint inherently register channels as it receives notifications? I am not calling any Pinpoint code to register channels myself.

nzayatz14 avatar Sep 13 '22 11:09 nzayatz14

@nzayatz14 ah yes, Pinpoint creates (or reuses) a notification channel whenever it receives a notification -- it should show up as "Notifications" in the device settings. Please try using that channel instead (should be no extra effort to create them on your side, just removing the code to create the additional channels) and seeing if the error goes away.

tjleing avatar Sep 13 '22 17:09 tjleing

@tjleing that is correct they show in device settings based on which notifications I receive. I needed to create the notification channels by hand in order to specify custom sounds/vibrations/etc per channel. Does the SDK have a way to do this or to disable pinpoints channel creation in order for me to configure the channels how I need them?

Example custom channel creation:

                val soundName = "my_custom_sound" //name of some mp3 file
                val packageName: String = applicationContext.packageName
                if (applicationContext.resources.getIdentifier(
                        soundName,
                        "raw",
                        packageName
                    ) != 0
                ) {
                    val resId: Int = applicationContext.resources.getIdentifier(
                        soundName,
                        "raw",
                        packageName
                    )
                    soundUri = Uri.parse("android.resource://$packageName/$resId")
                }

                val audioAttributes = AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build()

                val notificationChannel = NotificationChannel(
                    channel.getChannelId(applicationContext),
                    channel.channelName,
                    NotificationManager.IMPORTANCE_HIGH
                )
                notificationChannel.enableLights(true)
                notificationChannel.enableVibration(true)
                notificationChannel.setSound(soundUri, audioAttributes)
                notificationManager.createNotificationChannel(notificationChannel)

Thanks for the help!

nzayatz14 avatar Sep 13 '22 19:09 nzayatz14

@nzayatz14 in almost all cases I believe you'd want to create the NotificationChannel yourself with a channel ID matching the one that Pinpoint is looking for, "PINPOINT.NOTIFICATION". Then, instead of creating a brand-new channel for Pinpoint notifications, the library will simply use the one you have created.

In the unlikely event that the Pinpoint NotificationChannel is already created and you want to modify it in-place, you should be able to just grab it from the NotificationManager like so:

NotificationChannel notificationChannel = notificationManager.getNotificationChannel("PINPOINT.NOTIFICATION");

and then call notificationChannel.enableLights(true) etc. We fetch or create the NotificationChannel immediately before creating a notification, so it seems unlikely that this scenario will occur.

tjleing avatar Sep 13 '22 21:09 tjleing

@tjleing so as far as I can tell from the documentation, createNotificationChannel() should just update any existing channels if one already exists with the same name. So in theory there shouldn't be any issues of trying to duplicate channels. This is an odd bug then if the channels are being initiated twice haha

nzayatz14 avatar Sep 13 '22 21:09 nzayatz14

@nzayatz14 yep agreed, the method of just creating the new notification channel with the correct ID should cover all cases. Please let me know if it works for you!

tjleing avatar Sep 13 '22 21:09 tjleing

@tjleing that is what I am currently doing, which is why I find it strange that this bug is related to notification channel creation

nzayatz14 avatar Sep 13 '22 22:09 nzayatz14

@nzayatz14 I see, it's possible that that isn't the issue then. I'll look into it more (plus that "making it more bulletproof" that I was talking about above).

tjleing avatar Sep 15 '22 19:09 tjleing

@tjleing perfect, thank you!

nzayatz14 avatar Sep 15 '22 20:09 nzayatz14

@nzayatz14 can I ask what SDK version you upgraded from (and also if you changed the minSdkVersion at the same time)? I believe I've found a bug but I'm not sure if it'll fix your issue since the file hasn't been changed in a while. Thanks!

tjleing avatar Sep 16 '22 20:09 tjleing

@tjleing I updated from v2.47.0 to v2.51.0, but the issue may have existed in v2.47.0 as well. My minSdkVersion has been 24 and has not been updated for a very long time. So this very well may have been a longstanding issue.

nzayatz14 avatar Sep 16 '22 21:09 nzayatz14

Fix released with 2.54.0

tjleing avatar Oct 03 '22 17:10 tjleing