You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
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!
The text was updated successfully, but these errors were encountered:
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).
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!
The text was updated successfully, but these errors were encountered: