cranberrygame / cordova-plugin-pushnotification-parsepushnotification

17 stars 16 forks source link

App crashes when I reload the index.html #8

Open giovanni256 opened 9 years ago

giovanni256 commented 9 years ago

Hi Berry.

Excuse me for this message but I have a problem with the plugin.

When I add the code to the index.html every time that I open the app it shows the message of onRegisterAsPushNotificationClientSucceeded...

The problem is that if I go to another page of my app and then I return on the index.html the app crashes and I don't know why. I think that the problem is that the app tryies to register the app on Parse.com but it is already registered...

These are the errors reported by Google Play Store: java.lang.RuntimeException: Unable to start receiver com.parse.ParseBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference at android.app.ActivityThread.handleReceiver(ActivityThread.java:2602) at android.app.ActivityThread.access$1700(ActivityThread.java:147) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5253) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.parse.ParsePlugins$Android.applicationContext()' on a null object reference at com.parse.Parse.checkContext(Parse.java:440) at com.parse.Parse.getApplicationContext(Parse.java:270) at com.parse.ManifestInfo.getContext(ManifestInfo.java:324) at com.parse.ManifestInfo.getPackageManager(ManifestInfo.java:328) at com.parse.ManifestInfo.getPackageInfo(ManifestInfo.java:358) at com.parse.ManifestInfo.deviceSupportsGcm(ManifestInfo.java:446) at com.parse.ManifestInfo.getPushType(ManifestInfo.java:212) at com.parse.PushService.startServiceIfRequired(PushService.java:222) at com.parse.ParseBroadcastReceiver.onReceive(ParseBroadcastReceiver.java:19) at android.app.ActivityThread.handleReceiver(ActivityThread.java:2595) ... 9 more

What can I do? Thanks a lot!

anzovin-brian commented 9 years ago

@giovanni256 I just started encountering a crash very similar to this one. At the very least it generates the same crash report.

The problem is that there are scenarios where the app will be unloaded from memory but not in a way where the current implementation of the Cordova plugin is aware this has happened. In my case, the cause of the crash specifically was the user terminating the app manually, rather than leaving it open or having it get unloaded automatically by Android. It caused the process to be restarted when ParseBroadcastReceive's onReceive method was called but without Parse being reinitialized.

I've made a fork of this project that fixes this bug and adds a few other things necessary for the project I'm working on: https://github.com/anzovin-brian/cordova-plugin-pushnotification-parsepushnotification

You could give it a try and see if it fixes the crash for you. At some point when I have the chance I hope to set up a pull request to this repo to fix the Android crash.

giovanni256 commented 9 years ago

@anzovin-brian thanks a lot. I'm going to try if it works in my case.

I tell you how I have this error:

So I don't know if this is an error made by me or the app. Do you know if I have to put the API in each page of my app or only in the index.html?

I'm trying your plugin :+1:

giovanni256 commented 9 years ago

@anzovin-brian I'm using your plugin and it works very well! The only problem is that I receive push notifications only if the app is opened/is in background.

If it is closed I don't receive anything...

anzovin-brian commented 9 years ago

@giovanni256 You only want to initialize the plugin (i.e. call window.parsepushnotification.setUp) just once when your app loads. If you were calling it a second time when your app returned to index.html, that would explain the original crash.

One of the changes I made to my version of the plugin is that it keeps track of whether its initialized and only calls Parse's initialization function once, even if window.parsepushnotification.setUp is called again, so that might explain why it fixed your crash.

I'm not sure why you only receive push notifications when the app is open or in the background. I made these changes specifically because receiving push notifications when the app was closed would cause it to crash, and since making the changes I've confirmed that push notifications are received when the app is closed. Have you confirmed that your app has successfully subscribed to a channel?

giovanni256 commented 9 years ago

@anzovin-brian thanks a lot for your time.

Yes, I noticed that the app, with your plugin, doesn't crash and it works very great.

I don't understand what do you mean for "channel"... I just pasted this code in my index.html:

                   <script>
                            var applicationId = "MyAppID from Parse.com";
                        var clientKey = "MyClientKey from Parse.com";
                                document.addEventListener("deviceready", function(){
                                window.parsepushnotification.setUp(applicationId, clientKey);

        //registerAsPushNotificationClient callback (called after setUp)
        window.parsepushnotification.onRegisterAsPushNotificationClientSucceeded = function() {
            alert('Your account is now verified!');
        };

        window.parsepushnotification.onRegisterAsPushNotificationClientFailed = function() {
            alert('We can't verify your device, please contact an Admin.');
        };  

                    }, false);
                   </script>

What I have to do now? Thanks a lot! :)

anzovin-brian commented 9 years ago

I think you want to use window.parsepushnotification.subscribeToChannel('your channel here');

Call that in the onRegisterAsPushNotificationClientSucceeded callback function.

Ordinarily you register your app for a particular Parse channel, and then when you send the push notification if you specify that channel the app will receive it. There is a way to do it without using channels but I think you should probably give channels a try first.

furins commented 9 years ago

@anzovin-brian I'm using your fork on intel XDK and it works like a charm! thank you (and thanks also to @cranberrygame).

In your fork, however, I'm unable to remove the badges once they are set (iOS). I call

window.parsepushnotification.setBadgeNumber(0); 

after registering to the channel, in the deviceready event handler, but the badge remains. How can I solve this issue? thanks!

micik commented 9 years ago

window.parsepushnotification.setBadgeNumber(0);

not implemented in code

furins commented 9 years ago

@micik : It is in @anzovin-brian fork (I refer to that fork in my comment).

anzovin-brian commented 9 years ago

@furins You have to call setBadgeNumber in the 'onRegisterAsPushNotificationClientSucceeded' function that gets called after you initialize Parse:

function onSuccessfulSetup() {
    window.parsepushnotification.setBadgeNumber(0);
}

window.parsepushnotification.onRegisterAsPushNotificationClientSucceeded = onSuccessfulSetup;
window.parsepushnotification.setUp(parseApplicationId, parseClientKey);

I should add to anyone using my fork just to remember that it's something I did in a very ad hoc fashion and shouldn't necessarily be considered production ready code.

At some point I'll put together a pull request for moving this back into the main repo.

furins commented 9 years ago

thanks @anzovin-brian! The reason why I've posted my request here is exactly because I hope you'll submit a PR to @cranberrygame's repository in the future, so my question - and your answers - will be relevant soon also for other users of the "official" version of the plug-in.

thanks again, to you both.

AndresPF commented 8 years ago

Hi! @anzovin-brian I'm using Phonegap Build, how can I use your forked project? I can only install plugins using the config.xml file so I can't use:

cordova plugin add https://github.com/anzovin-brian/cordova-plugin-pushnotification-parsepushnotification

any suggestion? Thanks

amirul1000 commented 6 years ago

I am getting app crash on reload time.First time loading no crash.When the app getting crash the system is ok again.getting crash before splash screen.it seems memory problem.How can I resolve this