Hitman666 / cordova-ios-voip-push

Register and receive VoIP push notifications in hybrid app projects with Cordova
MIT License
23 stars 28 forks source link

Callback doesn't work when app is closed or in the background #1

Closed emin93 closed 7 years ago

emin93 commented 7 years ago

First of all, thanks a lot for all the work you've put into this library. I went through the tutorial and did everything necessary. Now it almost seems to work but there's one problem left. If the app is opened, i'll get to the notification callback and everything's fine. But when the app is closed (doesn't matter if completely closed or just in the background), it won't set the item in the localStorage. The next time i start the app, i'll get to the callback but that's too late.

I used the following code:

document.addEventListener('deviceready', function() {
    alert(localStorage.getItem("data"));

    var push = VoIPPushNotification.init();

    push.on('registration', function(data) {
        console.log(data);
        //data.deviceToken;
        //do something with the device token (probably save it to your backend service)
    });

    push.on('notification', function(data) {
        localStorage.setItem("data", JSON.stringify(data));
    });

    push.on('error', function(e) {
        console.error(e);
    });

}, false);

What part am i missing? Thanks in common.

Hitman666 commented 7 years ago

Hey Emin,

Thank you, I'm glad it you found it useful.

This is the same problem I ran into as well, and the problem is actually this line:

alert(localStorage.getItem("data"));

It seems that if the code logic which processes the VoIP push has any alert() it just "doesn't execute any further from that point". That's why when you have the app open and when you close the alert it goes through.

Now, please try to remove that line (or just console.log it) and let me know if this solves it for your.

Thanks for trying out the plugin and for your feedback, Nikola

emin93 commented 7 years ago

Hey Nikola,

Thank you for your fast answer. Actually the same thing just ran through my mind but sadly it didn't help. I removed the alert and checked the device logs for your NSLog ([objC] received VoIP msg: test) which is being called when the method didReceiveIncomingPushWithPayload gets called. But the log doesn't come until i start the app.

Thanks in common, Emin

Hitman666 commented 7 years ago

Ok, that's weird as in my case I can really attest that when I removed the alerts they did come through even when the app was like totally closed (not in the background). I was thinking about this issue a bit more actually as I got a note from a guy saying that even though it works, it stops after like 10minutes of being in the background - was wondering if adding some cordova background plugin would solve this.

emin93 commented 7 years ago

Hey Nikola,

This issue can now be closed. I just found out another plugin breaks this. I removed all other plugins and tried it with just your plugin active and it seems to work now. I'm trying to find out which one, but i'm sure now it doesn't have anything to do with your plugin.

Thank you again for your help and patience, Emin

Hitman666 commented 7 years ago

No problem, I'm glad you chimed in. Can I please trouble you to ping me which plugin was it once you find it?

emin93 commented 7 years ago

As i just found out, it is the Cordova WKWebView plugin which breaks it. I'm trying to find out if there's a way to fix that. I'll keep you up to date.

Hitman666 commented 7 years ago

Thank you!

emin93 commented 7 years ago

It seems there's no solution for that at the moment. The problem is, that the WKWebView pauses the JavaScript runtime when it's in the background. Until Apple decides to allow that in the future, there's no fix for that. I solved the problem for now by doing my stuff with custom Objective C code.

Hitman666 commented 7 years ago

Thank you for your update!

Could you please elaborate on the 'custom Objective C code'?

So, basically, if I have some kind of JavaScript code that is processing the incoming VoIP push, that will not get executed? I must say that that's not what I found when testing. I did, however, find that if you have alert popups it does not work until I open up the app.

However, I came across a post (can't find it now :/) where they said that if the app is longer than 10minutes in the background mode that then it goes into so-called 'deep sleep' and that the VoIP push won't come through - have you had the similar experience?

Thanks, Nikola

Hitman666 commented 7 years ago

Hey Emin,

One update on this. Take a look at this comment that I got on my post:

I’ve found your article very interesting, I’ve a doubt about push notification. I’ve noticed a bad behaviour when phone or iPad is suspended (locked with black screen). Sometimes maybe when the phone is suspended for a while (10 minutes) is not received immediately but after several minutes. Note I’ve tested a develop or testflight application not in distribution. Could this behaviour changes after releasing my app?

I answered like this:

Well, actually, this is something that I can confirm. It seems that the app goes into the so-called deep-sleep and that then one can’t do anything with it until it’s again opened. Now, I was wondering if setting the ‘Background mode’ option in XCode or maybe using one of the plugins like this one (https://github.com/katzer/cordova-plugin-background-mode) would actually help. Then again, it may be questionable if Apple would let you through to App store with this plugin :/ Have you maybe made some progress on this issue yourself?

I, unfortunately, haven't yet get the chance to try out the plugin for the background mode. But, then again, as I've mentioned, it's 'risky' of Apple rejecting the app. Or, it could indeed be that once the app is approved by Apple, and it has Background mode option included (and approved) it will actually not allow the app to go to the so-called deep-sleep... I can't know that yet since I don't have an app with VoIP functionality in the store (yet). Will update in case I come across anything else that will potentially be useful.

emin93 commented 7 years ago

Hey Nikola

Thank you for your update. What i meant by doing my stuff in objective c is that i'm not using JS anymore for the background stuff. It's not possible to run JS code with the WKWebView in the background (at the moment).

Hitman666 commented 7 years ago

So, let me get this straight; let's say my app is in the background and I receive a VoIP push and I want it to trigger some function that will do some processing on the 'Ionic side' - that will not be possible?

I'm asking because I was able to get this exact scenario working. However, I also noticed that if I had alert's in my Ionic code that then the app would 'wait' until I open it up. Then it would present me the alert, and only then it would continue it's execution further.

Anyways, not meaning to 'beat the dead horse', I'm just trying to get the conversation going in terms of finding new valuable information which would benefit us both, and the other potential readers of this thread.

emin93 commented 7 years ago

Excuse my delayed answer. Yes you got this right but only in combination with the WKWebView plugin: https://github.com/apache/cordova-plugin-wkwebview-engine

Right now Cordova is using the old UIWebView as default to render the content. When using the default rendering engine, everything works fine. But since the WKWebView is a lot faster (Google "WKWebView vs. UIWebView"), i don't want to to depend on the old UIWebView engine.

Hitman666 commented 7 years ago

Ah, thanks!, and sorry for a blatantly delayed reply :/

Good luck with your project!

andrewvmail commented 6 years ago

@emin93 checkout https://github.com/andrewvmail/cordova-plugin-ionic-webview

tbassett44 commented 5 years ago

@emin93 any chance of sharing the objective C code you made for this?