defold / extension-push

This is a Defold native extension which provides access to Push Notifications functionality on iOs and Android devices.
https://www.defold.com/extension-push/
MIT License
20 stars 16 forks source link

Fix NullPointerException #40

Closed AGulev closed 2 years ago

AGulev commented 2 years ago

We tested and found that some crashes were related to the activity/context issue.

But here is code from Android code in a place where extension crashes with NullPointer:

  public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
        notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
    }
java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4487)
  at android.app.ActivityThread.access$1500 (ActivityThread.java:301)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2169)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8595)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
Caused by: java.lang.NullPointerException: 
  at android.app.Notification.addFieldsFromContext (Notification.java:3114)
  at android.app.Notification.addFieldsFromContext (Notification.java:3107)
  at android.app.NotificationManager.fixNotification (NotificationManager.java:610)
  at android.app.NotificationManager.notifyAsUser (NotificationManager.java:601)
  at android.app.NotificationManager.notify (NotificationManager.java:535)
  at android.app.NotificationManager.notify (NotificationManager.java:511)
  at com.defold.push.LocalNotificationReceiver.onReceive (LocalNotificationReceiver.java:30)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4478)

It is possible only id notification is null or extras is null, I covered both cases in this fix