简体   繁体   English

Flutter 带有操作按钮的本地通知

[英]Flutter local notification with action buttons

I tried flutter local notification plugin in my flutter project its work fine on simple notifications but I need functionality of notifications with action buttons pls help me or suggest me to achieve this function.我在我的 flutter 项目中尝试了 flutter 本地通知插件,它在简单通知上工作正常,但我需要带有操作按钮的通知功能,请帮助我或建议我实现这个 function。

Unfortunately, the flutter_local_notifications plugin does not support action buttons yet.不幸的是,flutter_local_notifications 插件还不支持操作按钮。 There´safeature request to add this.有一个功能请求来添加这个。

I don´t know of any other plugins that support this, so i think right now the only possibility is to create your own custom plugin and develop it in native iOS code.我不知道有任何其他插件支持这一点,所以我认为现在唯一的可能性是创建您自己的自定义插件并在本机 iOS 代码中开发它。

使用 awesome_notifications: ^0.0.6+7 包

awesome_notifications can be used to push notifications with action buttons. awesome_notifications可用于通过操作按钮推送通知。

Explanation: 解释:
This can be done by customizing the data (Json) received like:这可以通过自定义接收到的数据 (Json) 来完成,例如:

{
    "to" : "[YOUR APP TOKEN]",
    "mutable_content" : true,
    "content_available": true,
    "priority": "high",
    "data" : {
        "content": {
            "id": 100,
            "channelKey": "big_picture",
            "title": "Huston!\nThe eagle has landed!",
            "body": "A small step for a man, but a giant leap to Flutter's community!",
            "notificationLayout": "BigPicture",
            "largeIcon": "https://media.fstatic.com/kdNpUx4VBicwDuRBnhBrNmVsaKU=/full-fit-in/290x478/media/artists/avatar/2013/08/neil-i-armstrong_a39978.jpeg",
            "bigPicture": "https://www.dw.com/image/49519617_303.jpg",
            "showWhen": true,
            "autoCancel": true,
            "privacy": "Private"
        },
        "actionButtons": [
            {
                "key": "REPLY",
                "label": "Reply",
                "autoCancel": true,
                "buttonType":  "InputField"
            },
            {
                "key": "ARCHIVE",
                "label": "Archive",
                "autoCancel": true
            }
        ],
        "schedule": {
            "timeZone": "America/New_York",
            "hour": "10",
            "minute": "0",
            "second": "0",
            "millisecond": "0",
            "allowWhileIdle": true,
            "repeat": true
        }
    }
}

and pushing notification with that messageData like:并使用该 messageData 推送通知,例如:

AwesomeNotifications().createNotificationFromJsonData(yourReceivedMapData);

You can use awesome_notifications package.您可以使用awesome_notifications package。

This how to create a notification with action buttons in it:这是如何创建带有操作按钮的通知:

AwesomeNotifications().createNotification(
  content: NotificationContent(
      id: 10,
      channelKey: 'basic_channel',
      title: 'Simple Notification',
      body: 'Simple body',
  ),
  actionButtons: <NotificationActionButton>[
    NotificationActionButton(key: 'accept', label: 'Accept'),
    NotificationActionButton(key: 'reject', label: 'Reject'),
  ],
);

You can add icons, text fields and so many other things.您可以添加图标、文本字段和许多其他内容。 Look here for more. 在这里查看更多信息。

And to listen for the tapped button, here's the code:要收听点击的按钮,代码如下:

AwesomeNotifications().actionStream.listen(
  (ReceivedAction receivedAction) {

    if (receivedAction.buttonKeyPressed == 'accept') {
      //Your code goes here
    } else if (receivedAction.buttonKeyPressed == 'reject') {
      //Your code goes here
    }

    //Here if the user clicks on the notification itself
    //without any button

  },
);

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

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