Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I got cold start notifications working! #410

Closed
zmandel opened this issue Feb 5, 2015 · 2 comments
Closed

I got cold start notifications working! #410

zmandel opened this issue Feb 5, 2015 · 2 comments

Comments

@zmandel
Copy link

zmandel commented Feb 5, 2015

Hey guys, according to the plugin instructions, onclick will not work from a cold start. However I managed to get it working. Works on Android 4.4.2, havent tried on iOS or other android versions but in theory it should work.

The idea is based on an observation that the callback for window.plugin.notification.local.onclick does get called on cold start but cant do many things. For example if you put "alert" on the callback it crashes the app on cold start, and many things like page navigation wont work either, making it appear as if onclick is not called, but it is (otherwise "alert" wouldnt be crashing the app when added to the callback).

So, I figured I could launch an activity from onclick to open a custom url-scheme that opens my app.
Install the custom url plugin: https://github.com/EddyVerbruggen/Custom-URL-scheme
then use this code from deviceready:
window.plugin.notification.local.onclick = onLocalNotification;

/* define this as a global function */
function onLocalNotification(id, state, json) {
var url = JSON.parse(json).url;
var strFind = "/www/";
var iFind = url.indexOf(strFind);
if (iFind >= 0) {
url = url.substr(iFind + strFind.length);
}
setTimeout(function () {
openUrlAsActivity("yourcustomscheme://" + url);
}, 1000);
//1000 or maybe longer is needed to reliably open the activity.
//Note it should be investigated how to reliably wait for the right time
//to open the activity. maybe another event needs to be waited instead of doing this setTimeout.
}

/* handleOpenURL will navigate to the custom url received */
function handleOpenURL(url) {
var strFind = "://";
var iFind = url.indexOf(strFind);
if (iFind < 0)
return;
url = url.substr(iFind + strFind.length);
$.mobile.changePage(url, { transition: "slidedown" });
}

//add a notification somewhere:
window.plugin.notification.local.add({
id: 1,
message: "hi",
json: JSON.stringify({ url: window.location.href }) //pass the current page url
});

//helper to open url as activity
function openUrlAsActivity(url) {
window.plugins.webintent.startActivity({
action: window.plugins.webintent.ACTION_VIEW,
url: url
},
function () { },
function (e) { alertMobile('Failed to open. Please try later'); }
);
}

so far its working great. Let me know if you find issues with other android or iOS versions. cheers!

@zmandel
Copy link
Author

zmandel commented Feb 6, 2015

FYI anyone interested reply here and I will post a more complete example. I had to make a few changes to make it work on android 4.0 and also on slow emulators. Not much more complicated than what I put above. I not have cold- start notifications that launch the app and execute the action, from 4.0 and above (and should work from 3.0 but I dont have a way to try it).

@katzer
Copy link
Owner

katzer commented Mar 11, 2015

The new 0.8.x release is a complete rewrite. If the issue still exists please reopen it.

You may also take a look into the new wiki.

Thanks!

@katzer katzer closed this as completed Mar 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants