I'm trying to pin some dynamic shortcuts of my app, they are going to be created when the user creates a Custom time.
I have two version of the code, which I am launching the code from a WebView using a JavaScriptInterface, but none of them is working as expected since one of them is trying to open the Play Store and the second one says: "the App doesn't exist" when I created the shortcut from the App.
This one is launching the Play Store:
[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
if (manager.IsRequestPinShortcutSupported)
{
try
{
//Create the new intent
var intent = new Intent(Intent.ActionView);
//Set the flag of the new task
intent.AddFlags(ActivityFlags.NewTask);
//Get the apps from the Play Store
intent.SetData(Android.Net.Uri.Parse("market://details?id=" + context.PackageName));
//Set the custom time as a variable
intent.PutExtra("customTime", time);
//Set the info of the shortcut
var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
.SetShortLabel("TM Timer")
.SetLongLabel("TM Timer")
.SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
.SetIntent(intent)
.Build();
//Set values
var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
intent, /* flags */ 0);
//Creates the shortcut
manager.RequestPinShortcut(info, successCallback.IntentSender);
}
catch (System.Exception ex)
{
}
}
}
This one is saying that the app doesn't exist:
[Export]
[JavascriptInterface]
public void PinCustomTime(string time)
{
var manager = context.GetSystemService(Context.ShortcutService) as ShortcutManager;
if (manager.IsRequestPinShortcutSupported)
{
try
{
//Set the info of the shortcut with the App to open
var info = new ShortcutInfo.Builder(context, $"tmTimer_{DateTime.Now.ToString("yyMMddHHmmss")}")
.SetShortLabel("TM Timer")
.SetLongLabel("TM Timer")
.SetIcon(Icon.CreateWithResource(context, Resource.Drawable.iconInv))
.SetIntent(new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName)))
.Build();
//Create the new intent
var intent = manager.CreateShortcutResultIntent(info);
intent.PutExtra("customTime", time);
//Set values
var successCallback = PendingIntent.GetBroadcast(context, /* request code */ 0,
intent, /* flags */ 0);
//Creates the shortcut
manager.RequestPinShortcut(info, successCallback.IntentSender);
}
catch (System.Exception ex)
{
}
}
}
I tried a third code, but that one was trying to open any app that wasn't my own app. Does anyone have experienced something similar? Or know what am I missing?
I have followed multiple tutorials and examples like these ones:
Thanks for your support.
P.S.:
When user clicks the shortcut, this intent will be launched:
new Intent(Intent.ActionView).SetData(Android.Net.Uri.Parse(context.PackageName))
To launch a specific activity, replace it with (in java):
Intent i = new Intent(context.getApplicationContext(), MainActivity.class);
i.setAction(Intent.ACTION_VIEW);