cordova-plugin-shortcuts-android
cordova-plugin-shortcuts-android copied to clipboard
How do I run a function on press?
I don't really understand the intent
, can you add a description or a demo for how can I run a js function when I press on a shortcut?
Also setDynamic
must run each time when you open the app, or 1 is enought, than when you want to update/change the shortcuts you have to run it again?
Thanks
Any way to do this?
I hope my experience with the plugin is useful to you.
I don't really understand the
intent
, can you add a description or a demo for how can I run a js function when I press on a shortcut?
This is an example code to set the shortcuts, I use a similar one in my App.
async function setShortcuts() { // Async function is optional
if(window.cordova.platformId == 'android')
{
window.plugins.Shortcuts.supportsDynamic(function(supported) {
if(supported)
{
var shortcuts = [];
shortcuts.push({
id: 'search',
shortLabel: 'Search something',
longLabel: 'Search something',
//iconBitmap: '<Bitmap for the shortcut icon, base64 encoded>',
//iconAdaptiveBitmap: '<Bitmap for the shortcut icon, base64 encoded>',
//iconFromResource: 'module', //filename w/o extension of an icon that resides on res/drawable-* (hdpi,mdpi..)
intent: {
action: 'search',
}
});
shortcuts.push({
id: 'news',
shortLabel: 'News with Id 9999',
longLabel: 'News with Id 9999',
//iconBitmap: '<Bitmap for the shortcut icon, base64 encoded>',
//iconAdaptiveBitmap: '<Bitmap for the shortcut icon, base64 encoded>',
//iconFromResource: 'module', //filename w/o extension of an icon that resides on res/drawable-* (hdpi,mdpi..)
intent: {
action: 'news',
extras: {
newsId: 9999, // Custom extras are also supported (boolean, number and string only)
}
}
});
window.plugins.Shortcuts.setDynamic(shortcuts, function(){}, function(err){});
}
}, function(err){});
}
}
And this one to get the events when opening the shortcut.
function processShortcutstIntents(intent) {
if(intent.action == 'app.package.name.search')
{
// Open search page
}
else if(intent.action == 'app.package.name.news')
{
if(intent.extras && intent.extras['app.package.name.newsId'])
{
var newsId = intent.extras['app.package.name.newsId'];
// Open news page
}
}
}
if(window.cordova.platformId == 'android')
{
window.plugins.Shortcuts.getIntent(processShortcutstIntents); // When the App is opened for the first time using the shortcut
window.plugins.Shortcuts.onNewIntent(processShortcutstIntents); // If you open a shortcut once the App is already open
}
Also
setDynamic
must run each time when you open the app, or 1 is enought, than when you want to update/change the shortcuts you have to run it again?
Currently I generate the shortcuts every time the App is opened (With a delay and an asynchronous function), so I cannot help you with this.