【问题标题】:HMS cancel notification with tag not working ( (HMS push kit)HMS 取消通知,标签不起作用((HMS 推送工具包)
【发布时间】:2020-12-24 03:06:54
【问题描述】:

我正在 Flutter 应用中集成华为 Push Kit (https://pub.dev/packages/huawei_push)。

我测试在我的华为手机中安排了每分钟通知一次的通知,并且我能够按预期收到通知。但是当我尝试使用 clearNotification() 函数(如下所示)取消通知时,通知似乎没有取消。想知道我错过了什么吗?

scheduleLocalNotification() async {
    try {
      Map<String, dynamic> localNotification = {
        HMSLocalNotificationAttr.TITLE: 'Notification Title',
        HMSLocalNotificationAttr.MESSAGE: 'Notification Message',
        HMSLocalNotificationAttr.TICKER: "OptionalTicker",
        HMSLocalNotificationAttr.TAG: "push-tag",
        HMSLocalNotificationAttr.BIG_TEXT: 'This is a bigText',
        HMSLocalNotificationAttr.SUB_TEXT: 'This is a subText',
        HMSLocalNotificationAttr.LARGE_ICON: 'ic_launcher',
        HMSLocalNotificationAttr.SMALL_ICON: 'ic_notification',
        HMSLocalNotificationAttr.COLOR: "white",
        HMSLocalNotificationAttr.VIBRATE: true,
        HMSLocalNotificationAttr.VIBRATE_DURATION: 1000.0,
        HMSLocalNotificationAttr.ONGOING: false,
        HMSLocalNotificationAttr.DONT_NOTIFY_IN_FOREGROUND: false,
        HMSLocalNotificationAttr.AUTO_CANCEL: false,
        HMSLocalNotificationAttr.INVOKE_APP: false,
        HMSLocalNotificationAttr.ACTIONS: ["Yes", "No"],
        HMSLocalNotificationAttr.REPEAT_TYPE: RepeatType.MINUTE,
        HMSLocalNotificationAttr.FIRE_DATE:
            DateTime.now().add(Duration(minutes: 1)).millisecondsSinceEpoch,
      };
      Map<String, dynamic> response =
          await Push.localNotificationSchedule(localNotification);
      print("Scheduled a local notification: " + response.toString());
    } catch (e) {
      print("Error: " + e.toString());
    }
  }  

clearNotification() async {
    Push.cancelNotificationsWithTag('push-tag');
  } 

【问题讨论】:

    标签: push-notification huawei-mobile-services


    【解决方案1】:

    目前华为推送服务不支持clearNotification()功能。您可以使用发送下行链路消息 API。有一个auto_clear参数,可以设置消息显示时长,单位为毫秒。持续时间到期后,消息会自动删除。 请参考以下代码:

    {
        "validate_only": false,
        "message": {
            "android": {
                "notification":{"body":"Big Boom Body!","click_action":{"type":3},"title":"Big Boom Title","auto_clear": 360000}
                },
        "token":[
        "token1",
        "token2"]
        }
    }
    

    欲了解更多信息,请参阅docs

    【讨论】:

      【解决方案2】:

      调用 Push.cancelNotificationWithTag('push-tag') 的 clearNotification 函数是异步的。 可能性是 Push.cancelNotificationWithTag 之前可能会被调用。 直接调用它而不是通过异步函数或添加等待。

      【讨论】:

        猜你喜欢
        • 2021-12-22
        • 2020-12-19
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        • 2021-12-19
        • 2017-01-25
        • 2020-05-12
        • 1970-01-01
        相关资源
        最近更新 更多