繁体   English   中英

从通知操作导航 Flutter 应用程序

[英]Navigate Flutter App from Notification Action

我想显示一个包含一个或多个操作的通知,当按下这些按钮时,我想导航到 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 扩展了 BroadcastReceiver 并且它应该作用于按钮点击

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 应用程序的 AndroidManifest.xml 中添加了以下内容

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

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

Android:

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

Flutter:

  1. 在声明应用程序行为以进行通知处理的main function 调用方法中:
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或其他 state 管理来更改应用程序中的当前屏幕(将当前屏幕存储在 model 中,当您处理通知时,更改屏幕和刷新状态)。

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

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)
        }
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM