简体   繁体   中英

Is it possible to navigate to page from native code onFlutter?

I want to navigate to my inbox page when clicking the notification. I handle notification on native code. I want to do this when clicking the notification. Navigator.pushNamed(context,'/inboxPage')

Android Native

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT)

val channelId = getString(R.string.default_notification_channel_id)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.app_icon)
        .setContentTitle(messageTitle)
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent)

IOS Native

    let notificationContent = UNMutableNotificationContent()
    notificationContent.title = userInfo["title"] as! String
    notificationContent.body = userInfo["body"] as! String
    notificationContent.sound = UNNotificationSound.default()
    let request = UNNotificationRequest(identifier: UUID.init().uuidString, content: notificationContent, trigger: nil)
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error: Error?) in
        if let theError = error {
            print("error \(theError)")
        }
    }

define a global var for a listener function

Function kNotificationClicked;

instantiate/init a method channel

  const platform = MethodChannel('com.myapp.etc');
  platform.setMethodCallHandler((call) async {
    if (call.method.compareTo('notification-clicked') == 0) {
      if(kNotificationClicked!=null){
       kNotificationClicked(call.arguments);
     }
    }
  });

in any widget (mostly while init or even better while the app init) set the callback

kNotificationClicked = (args){
  //go to the route
}

on native side(kotlin) instantiate a method channel


    private val CHANNEL = "com.myapp.etc"
    protected lateinit var platform: MethodChannel;

    fun configureFEngine() {
        platform = MethodChannel(flutterView, CHANNEL)
        platform.setMethodCallHandler { call, result ->
        }
    }

invoke when you wish

   fun notificationClicked() {
        platform.invokeMethod("notification-clicked", args)
    }

I've not tested the code, but have shown you how would i approach this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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