我在我的flutter项目中尝试使用本地通知插件,它可以正常地使用简单的通知,但我需要包含操作按钮的通知功能,请帮助我或建议我如何实现此功能。
我在我的flutter项目中尝试使用本地通知插件,它可以正常地使用简单的通知,但我需要包含操作按钮的通知功能,请帮助我或建议我如何实现此功能。
很遗憾,flutter_local_notifications插件目前还不支持操作按钮。有一个功能请求正在进行中。
我不知道是否有其他插件支持此功能,因此我认为现在唯一的可能是创建自己的定制插件并使用原生iOS代码进行开发。
使用 awesome_notifications: ^0.0.6+7 包
awesome_notifications 可用于推送带有操作按钮的通知。
说明:
可以通过自定义接收到的数据(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
}
}
}
AwesomeNotifications().createNotificationFromJsonData(yourReceivedMapData);
你可以使用awesome_notifications插件。
以下是创建具有操作按钮的通知的步骤:
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'),
],
);
您可以添加图标、文本字段和许多其他内容。请在此处查看更多信息。
要监听被点击的按钮,请使用以下代码:
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
},
);