HarmonyOS鸿蒙Next中Flutter项目点击推送通知后进入到app的指定页面应该怎么实现?

HarmonyOS鸿蒙Next中Flutter项目点击推送通知后进入到app的指定页面应该怎么实现? Flutter项目已经获取了推送服务的pushToken,后端也发送了消息推送,我需要点击推送的通知进入到app的指定页面,具体应该怎么实现?有demo吗?

4 回复

【背景知识】

  • HarmonyOS提供Push Kit(推送服务的基础能力),包括获取和删除推送服务Token、绑定和解绑账号和接收场景化消息的功能。
  • Flutter通过channel的方式调用HarmonyOS侧的API方法可获取到推送服务的Token。

【解决方案】

  1. ArkTS侧调用推送服务API实现方法:
  • 使用push_kit作为标识符,Flutter端可以通过这个标识符来调用或者接收来至HarmonyOS侧的数据。
  • 调用pushService.getToken()的API方法从HarmonyOS侧获取推送token。
  • 使用invokeMethod方法将从Flutter端接收到的pushKit方法名以及HarmonyOS侧获取到的推送Token传递回去,即Flutter端可以接收这个推送令牌来注册推送服务。
async onAttachedToEngine(binding: FlutterPluginBinding): Promise<void> {
  this.channel = new MethodChannel(binding.getBinaryMessenger(), 'push_kit');
  // 处理来自Flutter端的调用。
  const pushToken: string = await pushService.getToken();
  this.channel.invokeMethod('pushKit', pushToken);
  console.info("1111PushKitPlugin onAttachedToEngine")
}
  1. Flutter侧接收Token的实现方法:
  • setupMethodChannel()用于设置MethodChannel,即用于Flutter与ArkTS代码之间的桥梁。
  • 静态常量_channel类型为MethodChannelpush_kit标识符可被ArkTS代码识别并用来发送请求到 Flutter端。
Future<void> _runMethod() async {
  await MyMethodChannelHandler.setupMethodChannel();
}
class MyMethodChannelHandler {
  static const MethodChannel _channel = MethodChannel('push_kit');

  static Future<void> setupMethodChannel() async {
    _channel.setMethodCallHandler(_onMethodCall);
  }

  static Future<dynamic> _onMethodCall(MethodCall call) async {
    switch (call.method) {
      case 'pushKit':
      // 处理HarmonyOS侧调用的方法
        final String token = call.arguments;
        print('Received method call with argument: $token');
        // 返回结果给HarmonyOS侧
        return 'Response from Flutter';
      default:
        throw PlatformException(
          code: 'Unimplemented',
          message: 'The method ${call.method} has not been implemented.',
        );
    }
  }
}

更多关于HarmonyOS鸿蒙Next中Flutter项目点击推送通知后进入到app的指定页面应该怎么实现?的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


将推送消息的内容通过methodChannel传到flutter端,然后解析消息做页面跳转不就可以了

在HarmonyOS Next中实现Flutter项目点击推送通知跳转至指定页面,需使用HarmonyOS的Want机制。通过NotificationRequest设置Want参数,定义目标Ability和页面路径。在Flutter侧,通过Platform Channel接收Want参数,解析后使用Navigator.pushNamed跳转目标路由。需在Flutter路由表中注册目标页面,并确保Ability的配置正确。推送服务使用HarmonyOS的NotificationManager,携带的Want数据需包含页面标识。

在HarmonyOS Next中实现Flutter推送通知跳转指定页面,需要结合HarmonyOS推送服务和Flutter路由机制:

  1. 配置推送参数: 在发送推送时携带页面路由参数,例如:
{
  "title": "通知标题",
  "content": "通知内容",
  "route": "/detail",
  "id": "123"
}
  1. HarmonyOS侧处理: 在Ability中重写onStart方法接收推送数据:
@Override
protected void onStart(Intent intent) {
    super.onStart(intent);
    String route = intent.getStringParam("route");
    String id = intent.getStringParam("id");
    // 将参数传递给Flutter
}
  1. Flutter侧接收: 在Flutter中通过MethodChannel接收参数并跳转:
static const platform = MethodChannel('com.example/push');
platform.setMethodCallHandler((call) async {
  if (call.method == 'onPushClick') {
    String route = call.arguments['route'];
    String id = call.arguments['id'];
    Navigator.pushNamed(context, route, arguments: id);
  }
});
  1. 页面路由配置: 在Flutter中定义命名路由:
MaterialApp(
  routes: {
    '/detail': (context) => DetailPage(),
  },
);

实现要点:

  • 确保推送消息包含完整的路由参数
  • HarmonyOS与Flutter通过MethodChannel进行数据传递
  • 处理应用不同状态(前台/后台/终止)的跳转逻辑

目前官方尚未提供完整Demo,但可以参考HarmonyOS推送服务文档和Flutter路由文档进行实现。关键是要保证参数在Native层和Flutter层的正确传递。

回到顶部