Wizcorp / phonegap-plugin-localNotifications

PhoneGap plugin for providing native local notification system to JavaScript.
MIT License
38 stars 29 forks source link

Can't fire notification when application is closed #14

Closed rodolphorosa closed 9 years ago

rodolphorosa commented 10 years ago

I've tried to install this plugin and it worked fine while application was on, but, when application is closed, the notification is not fired in android 2.3 (in android 4.3 it's fired but the logcat shows many errors concerning to AlarmReceiver). I would like to know how this solve this problem. I'm using phonegap 3.4.0.

ps.: sorry for my bad english :)

fean commented 10 years ago

Not a problem, can you please post the stacktrace so that we can take a look at the error.

rodolphorosa commented 10 years ago

03-26 14:03:58.496: E/AndroidRuntime(16295): FATAL EXCEPTION: main 03-26 14:03:58.496: E/AndroidRuntime(16295): java.lang.RuntimeException: Unable to start receiver jp.wizcorp.phonegap.plugin.localNotification.AlarmReceiver: java.lang.NullPointerException 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2372) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.app.ActivityThread.access$1500(ActivityThread.java:149) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.os.Handler.dispatchMessage(Handler.java:99) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.os.Looper.loop(Looper.java:153) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.app.ActivityThread.main(ActivityThread.java:4987) 03-26 14:03:58.496: E/AndroidRuntime(16295): at java.lang.reflect.Method.invokeNative(Native Method) 03-26 14:03:58.496: E/AndroidRuntime(16295): at java.lang.reflect.Method.invoke(Method.java:511) 03-26 14:03:58.496: E/AndroidRuntime(16295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) 03-26 14:03:58.496: E/AndroidRuntime(16295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) 03-26 14:03:58.496: E/AndroidRuntime(16295): at dalvik.system.NativeStart.main(Native Method) 03-26 14:03:58.496: E/AndroidRuntime(16295): Caused by: java.lang.NullPointerException 03-26 14:03:58.496: E/AndroidRuntime(16295): at jp.wizcorp.phonegap.plugin.localNotification.AlarmReceiver.onReceive(AlarmReceiver.java:71) 03-26 14:03:58.496: E/AndroidRuntime(16295): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2365) 03-26 14:03:58.496: E/AndroidRuntime(16295): ... 10 more

These are the error that are shown in log cat when notification is fired and app is closed.

fean commented 10 years ago

I'll look info it. First I Will try recreate the environment. Can you tell me which options you set in the JavaScript function?

rodolphorosa commented 10 years ago

I used the same function that is in the code posted on github with a small adaptation (just for a brief test):

function addNotification( scheduleImmediately ) { var msg = prompt("Please enter a notification message:","A coleta chegou!"); if ( msg ) { var seconds = parseInt(prompt("Please enter a time interval in seconds (integer):", 30)); if ( seconds ) { var badge = parseInt(prompt("Please enter a badge value (integer):", 1)); if ( badge ) { var options = { "message" : msg, "seconds" : seconds, "badge" : badge, }; if ( scheduleImmediately ) { window.localNotification.add("GAME_NOTIFICATION", options); alert("Successfully scheduled:\n" + JSON.stringify(options)); } else { window.localNotification.queue("COME_BACK_AND_PLAY", options); alert("Successfully queued (will be scheduled after quitting the app):\n" + JSON.stringify(options)); } } } } }

rodolphorosa commented 10 years ago

The cordovaExample has the same errors

aogilvie commented 10 years ago

I've tested on Xperia 10x running 2.3.3 and not seeing any issues. The only changes I made was to add android-support-v13.jar and Notification notification = new Notification.Builder(context) to Notification notification = new NotificationCompat.Builder(context)

(import android.support.v4.app.NotificationCompat;)

The next PR is updating the example project, can you give that a try on your 2.3 device?

fean commented 10 years ago

I'm also not getting any errors, maybe the solution @aogilvie proposed also works in your case. Let us know if it does.

rodolphorosa commented 10 years ago

Is it working when application is closed? The only problem is that when I close the application the notification is not fired, and it only happens with 2.3 (in 4.3 a message is shown: unfortunaty, your application was closed). It seems to me that AlarmReceiver needs to be open so that the it works.

rodolphorosa commented 10 years ago

I've followed all this steps, but it keeps not working. It show the same errors.

aogilvie commented 10 years ago

@rodolphorosa Do you mean closed app or background state?

Some extra steps incase you cannot find android-support-v13.jar :

rodolphorosa commented 10 years ago

I've added this library to my project, but it keeps showing the same errors. And I mean closed app.

aogilvie commented 10 years ago

@rodolphorosa I have found the issue. I'm working on the fix. The problem is that the BroadcastReceiver gets the notification but tries to call LocalNotification.getCordovaWebView().sendJavascript("cordova.fireDocumentEvent('receivedLocalNotification', { active : true, notificationId : " + notificationId + " })"); of course, this is producing a null error. The fix is to properly boot the application from cold and pass the state openFromNotification in an intent to LocalNotification.class.

Gubolin commented 10 years ago

I also experience this problem. How long will the fix take? If I understand @aogilvie correctly, the plugin fires an event and is unsuccessful. I by myself don't need this event, so it could be simply deleted from the code to remove this error.

aogilvie commented 9 years ago

Fixed with https://github.com/Wizcorp/phonegap-plugin-localNotifications/commit/34ba521184032bceb1f615401906546b8beced80 Application no longer crashes. But application cannot yet receive the notification from cold closed. Will close this and open a enhancement issue. (Updated on develop and master)