Search code examples
androidflutterkotlinflutter-platform-channelpinned-shortcut

How to create android pinned shortcuts of chats in my flutter app?


I am coding a chat app for my project. I want to include the WhatsApp like functionality where we can create home screen shortcuts of chats by clicking on Add shortcut.

There was no plugin so I thought of using PlatformChannel and invoking native Kotlin code using it. But I don't know how to code native android in Kotlin so I searched for the code to the same.

I found official docs but the example their does not work on its own as it assumes their is already a shortcut with an id of 'shortcut-id' i looked up other places like old questions here, YouTube, many blogs website but none have a clear solution or code on how to create a new pinned shortcut and some which have are either old deprecated or in java their is no Kotlin solution.

Official docs code which does not work on its own

val shortcutManager = getSystemService(ShortcutManager::class.java)

if (shortcutManager!!.isRequestPinShortcutSupported) {
// Assumes there's already a shortcut with the ID "my-shortcut".
// The shortcut must be enabled.
val pinShortcutInfo = ShortcutInfo.Builder(context, "my-shortcut").build()

// Create the PendingIntent object only if your app needs to be notified
// that the user allowed the shortcut to be pinned. Note that, if the
// pinning operation fails, your app isn't notified. We assume here that the
// app has implemented a method called createShortcutResultIntent() that
// returns a broadcast intent.
val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(pinShortcutInfo)

// Configure the intent so that your app's broadcast receiver gets
// the callback successfully.For details, see PendingIntent.getBroadcast().
val successCallback = PendingIntent.getBroadcast(context, /* request code */ 0,
        pinnedShortcutCallbackIntent, /* flags */ 0)

shortcutManager.requestPinShortcut(pinShortcutInfo,
        successCallback.intentSender)}

So it will be of great help if you can give your valuable time and give me a way to work on or place where I might get the answer.


Solution

  • Yea your right I also researched and found the same. The best way is use platform channels and call the pinned shortcuts from their aka make your own plugin. That's a good idea though you can post it to pub dev of flutter.

    You can wait if someone else comes up with the functionality or just use some alternative way for making the feature you are using.