Agasper / unity-android-notifications

Unity3D Plugin for Android local notifications with example project
Other
555 stars 184 forks source link

Push notification crash when app is closed #83

Closed PinguN00t closed 6 years ago

PinguN00t commented 6 years ago

I got a problem with Unity 5.5.5p1 & Android oreo (don't know if it impact more Unity versions or Android versions)

Log :

02-10 10:29:14.805 16199 16199 I FirebaseInitProvider: FirebaseApp initialization unsuccessful 02-10 10:29:14.839 16199 16199 D AndroidRuntime: Shutting down VM 02-10 10:29:14.850 16199 16199 E AndroidRuntime: FATAL EXCEPTION: main 02-10 10:29:14.850 16199 16199 E AndroidRuntime: Process: com.company.game, PID: 16199 02-10 10:29:14.850 16199 16199 E AndroidRuntime: java.lang.RuntimeException: Unable to start receiver net.agasper.unitynotification.UnityNotificationManager: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3259) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.app.ActivityThread.-wrap17(Unknown Source:0) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:105) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6541) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at net.agasper.unitynotification.UnityNotificationManager.CreateChannel(UnityNotificationManager.java:41) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at net.agasper.unitynotification.UnityNotificationManager.createChannelIfNeeded(UnityNotificationManager.java:64) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at net.agasper.unitynotification.UnityNotificationManager.onReceive(UnityNotificationManager.java:154) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: at android.app.ActivityThread.handleReceiver(ActivityThread.java:3252) 02-10 10:29:14.850 16199 16199 E AndroidRuntime: ... 8 more

Even with the test scene. I solved it (apparently) by trasnfering the argument Context context from onReceive to CreateChannel :

    public static void CreateChannel(Context context, String identifier, String name, String description, int importance, String soundName, int enableLights, int lightColor, int enableVibration, long[] vibrationPattern, String bundle) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
        return;

    channels.add(identifier);
    NotificationManager nm = null;
    if(UnityPlayer.currentActivity != null)
    {
                nm = (NotificationManager) UnityPlayer.currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
    }
    else if (context != null)
    {
        nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

1/ I don't know if it's the way to go 2/ I don't know if it will be efficient in all the cases

But maybe it will help somebody / help me when you'll say that's was mistake from me !

Agasper commented 6 years ago

I will be very appreciated if someone can test if and make a pull request

sunxinzhe2012 commented 6 years ago

@Agasper a simple method to test,in demo click"action one time 5 sec",and quickly quit app,the app crash info will display and notification will fail

nikokahkonen commented 6 years ago

I faced this same issue with Android 8.1 and made a fix for it. Please see pull request: https://github.com/Agasper/unity-android-notifications/pull/89

Agasper commented 6 years ago

Merged