[英]Flutter local notification with action buttons
我在我的 flutter 項目中嘗試了 flutter 本地通知插件,它在簡單通知上工作正常,但我需要帶有操作按鈕的通知功能,請幫助我或建議我實現這個 function。
不幸的是,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
}
}
}
並使用該 messageData 推送通知,例如:
AwesomeNotifications().createNotificationFromJsonData(yourReceivedMapData);
您可以使用awesome_notifications package。
這是如何創建帶有操作按鈕的通知:
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
},
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.