android 带有操作按钮和图像的Flutter本地通知

j2cgzkjk  于 5个月前  发布在  Android
关注(0)|答案(2)|浏览(72)

我想用Firebase消息推这种类型的通知.现在的时间,我用https://pub.dev/packages/flutter_local_notifications这个包的正常通知.但我没有看到有一种方法来添加一个动作按钮.以及我想删除后45秒或另一个Firebase消息此通知.以及我正在开发Android和iOS的应用程序.如果我的问题不清楚或需要更多信息请自由评论。


的数据
我在Stackoverflow上看到了类似的问题。

ni65a41a

ni65a41a1#

flutter_local_notification还没有支持ticket上提到的通知操作按钮。你可能想考虑使用awesome_notifications插件,因为它支持通知按钮。
要使用操作按钮显示通知,只需使用

void _showNotificationWithButton() {
  AwesomeNotifications().createNotification(
    content: NotificationContent(
        id: 10,
        channelKey: 'basic_channel',
        title: 'Simple Notification',
        body: 'Simple body'),
    actionButtons: <NotificationActionButton>[
      NotificationActionButton(key: 'yes', label: 'Yes'),
      NotificationActionButton(key: 'no', label: 'No'),
    ],
  );
}

字符串
然后,您可以通过在NotificationButton上添加icon属性来为Action Button添加图标。该图标应该是资源中Map的图像的String资源。
要监听Action Button按下,请使用actionStream监听器。

AwesomeNotifications().actionStream.listen((receivedNotification) {
  // prints the key of the NotificationActionButton pressed
  debugPrint('Notification key pressed: ${receivedNotification.buttonKeyPressed}');
});

dy1byipe

dy1byipe2#

答案由两部分组成:
1.正在创建提示通知。
1.在通知中显示操作按钮。

1**创建提醒

我们将使用这个包awesome_notifications
这就是如何在项目的main()方法中初始化它。

AwesomeNotifications().initialize(
  // set the icon to null if you want to use the default app icon
  null,
  [
    NotificationChannel(
      channelGroupKey: 'channel_group_key',
      channelKey: 'channel_key',
      channelName: 'channel_name',
      channelDescription: 'channel_description',
      importance: NotificationImportance.Max,
    )
  ],
  debug: true,
);

字符串
仅限Android初始化:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example">

  <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
  <application>
    ...
     <activity
       android:name=".MainActivity"
       android:showOnLockScreen="true"
       android:showWhenLocked="true"
       android:turnScreenOn="true">
       ...
     </activity>
   ...
  </application>
</manifest>

**注意:**在iOS上,您需要请求通知权限。在Android 11上,您需要请求fullScreenIntent。代码如下(放在initState()或其他文件中):

AwesomeNotifications().isNotificationAllowed().then(
  (isAllowed) {
    //It would be more appropriate if you can show your own dialog
    //to the user before requesting the notifications permissons.
    if (!isAllowed) {
      AwesomeNotifications().requestPermissionToSendNotifications(
        permissions: [
          NotificationPermission.Alert,
          NotificationPermission.Sound,
          NotificationPermission.Badge,
          NotificationPermission.Vibration,
          NotificationPermission.Light,
          NotificationPermission.FullScreenIntent,
        ],
      );
    }
  }
);


以及如何创建通知,把它每当你需要消防通知:

AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: 10,
    channelKey: 'channel key' //Same as above in initilize,
    title: title,
    body: body,
    wakeUpScreen: true,
    fullScreenIntent: true,
    criticalAlert: true,
    //Other parameters
  ),
);

2**在通知中显示操作按钮

在前面的代码片段中,当创建通知时,您可以向其添加操作按钮,如下所示:

AwesomeNotifications().createNotification(
  //...
  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

  },
);

awesome_notifications包非常广泛,我建议您从他们的页面了解更多信息。

相关问题