【问题标题】:Navigate Flutter App from Notification Action从通知操作导航 Flutter 应用程序
【发布时间】:2021-05-20 11:54:09
【问题描述】:

我想显示一个包含一个或多个操作的通知,当按下这些按钮时,我想导航到 Flutter 应用程序中的特定路线,即使应用程序已关闭。 (我从来没有真正使用过本机代码。) 我通过消息渠道创建通知:

//Code shortened but leading code is irrelevant, as this code does show a notification
val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)

val pLaunchIntent = PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT)

val buttonIntent = Intent(context, ButtonReceiver::class.java);

val pButtonIntent = PendingIntent.getBroadcast(context, 0, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);

val builder = NotificationCompat.Builder(context, "My_Id")
.setSmallIcon(smallIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pLaunchIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setColor(Color.CYAN)
.addAction(smallIcon, "Test", pButtonIntent)

val someNotificationId = 1;

notify(someNotificationId, builder.build())

这个类扩展了一个广播接收器,它应该作用于按钮点击

class ButtonReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val launchIntent = context!!.packageManager.getLaunchIntentForPackage(context.packageName)
    PendingIntent.FLAG_UPDATE_CURRENT)
    context.startActivity(launchIntent)
    FlutterNotificationPlugin.channel.invokeMethod("navigate", "/route")
    Log.i("Notifications", "Invoked method")
    }
}

我的想法是,当通过动作点击启动应用程序时,我首先启动 Flutter 应用程序,然后调用一个方法告诉 Flutter 应用程序导航到相应的路由。频道本身可以工作,但是当我单击该操作时,什么也没有发生。应用未启动。

我还在 Flutter App 的 AndroidManifest.xml 中添加了以下内容

<receiver android:name="me.flutter_notification_plugin.ButtonReceiver"/>

【问题讨论】:

    标签: android flutter flutter-navigation


    【解决方案1】:

    您需要在onCreateonNewIntent 方法中处理来自MainActivity(您的应用程序的主要活动)中通知的意图,并从Flutter 端调用方法。例如:

    安卓:

    1. 声明处理意图的方法:
    private fun handleIntent(intent: Intent) {
        methodChannel?.invokeMethod(
            "notificationClick",
            mapOf(
                "notificationData" to intent.getData(),
            )
        )
    }
    
    1. 将方法调用添加到 onCreateonNewIntent 并从方法中放入 handleIntent 意图。

    颤动:

    1. main 函数调用方法中声明应用行为以进行通知处理:
    void main() {
      // Code before app start
      initializeNotificationHandler();
      // Start app after this comment
    }
    
    void initializeNotificationHandler() {
      WidgetsFlutterBinding.ensureInitialized();
      if (isAndroid()) {
        const methodChannel = MethodChannel('com.yourcompanyname.notifications/handle');
        methodChannel.setMethodCallHandler((call) async {
          if (call.method == 'notificationClick') {
            final data = call.arguments['notificationData'];
            jumpToPage(data['pageToOpen']);
          }
        });
      }
    }
    

    注意:您可以使用Provider或其他状态管理来更改应用程序中的当前屏幕(将当前屏幕存储在模型中,当您处理通知时,更改屏幕和刷新状态)。

    更新:这是 Android 端 MainActivity 类的示例:

    class MainActivity : FlutterActivity() {
    
        private val CHANNEL = "com.exampleapp.channel"
    
        private var methodChannel: MethodChannel? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            if (intent?.action == ACTION_NOTIFICATION_CLICK) {
                Handler(Looper.getMainLooper()).post {
                    handleNotificationIntent(intent, false)
                }
            }
        }
    
        private fun handleIntent(intent: Intent) {
            methodChannel?.invokeMethod(
                "notificationClick",
                mapOf(
                    "notificationData" to intent.getData(),
                )
            )
        }
    
        override fun onNewIntent(intent: Intent) {
            super.onNewIntent(intent)
            if (intent.action == EXTRA_SERVICE_ACTION_NOTIFICATION_CLICK) {
                handleNotificationIntent(intent, true)
            }
        }
    
    }
    

    【讨论】:

    • 感谢您的快速响应,但是,由于代码存在于 Flutter 插件中,因此“入口点”不会扩展 FlutterActivity,而是扩展 FlutterPlugin 和 MethodCallHandler。你也知道它是怎么工作的吗?
    • 你能解释一下“入口点”是什么意思吗?
    • 通常情况下,您会拥有名为 MainActivity 的 Kotlin 或 Java 类。但是在 Flutter Plugin 中,没有这样的类。相反,有一个类扩展了 FlutterPlugin 和 MethodCallHandler,它覆盖了 onAttachedToEngine 和 onMethodCall 方法,但是没有 onCreate。
    • 您有一个从FlutterActivity 扩展的活动,您可以使用它来处理通知中的意图。
    • 很抱歉这么晚了再次联系你,但我无法让它工作。你介意发布一个主 kotlin 文件的 sn-p 吗?
    猜你喜欢
    • 2019-02-10
    • 2020-12-28
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    • 2018-06-04
    • 2021-11-18
    相关资源
    最近更新 更多