任何事件都无法识别颤振本地通知按钮操作

问题描述 投票:0回答:1

我正在尝试为颤振本地通知显示的通知添加操作。不过,我已经添加了操作,操作按钮也显示在 android 和 ios 上,但点击该操作按钮时没有触发任何事件。

完整代码如下

``//初始化:- 静态常量初始化设置Android = AndroidInitializationSettings('@drawable/notification');

static DarwinInitializationSettings initializationSettingsDarwin =
  DarwinInitializationSettings(
      requestAlertPermission: true,
      requestBadgePermission: true,
      requestSoundPermission: true,
      notificationCategories: [
    DarwinNotificationCategory(
      '',
      actions: <DarwinNotificationAction>[
        DarwinNotificationAction.plain('some_action', 'Some Action'),
      ],
      options: <DarwinNotificationCategoryOption>{
        DarwinNotificationCategoryOption.customDismissAction,
      },
    ),
  ]);

final initializationSettings = InitializationSettings(
  android: initializationSettingsAndroid,
  iOS: initializationSettingsDarwin);

flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveNotificationResponse:
      (NotificationResponse notificationResponse) {
        log('Notification onDidReceiveNotificationResponse');
  },
  onDidReceiveBackgroundNotificationResponse: (NotificationResponse notificationResponse) {
    log('Notification onDidReceiveBackgroundNotificationResponse');
  }
);


final AndroidFlutterLocalNotificationsPlugin? androidImplementation =
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
    AndroidFlutterLocalNotificationsPlugin>();

final bool? granted = await androidImplementation?.requestPermission();
log('granted app $granted');


await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
        AndroidFlutterLocalNotificationsPlugin>()
    ?.createNotificationChannelGroup(notificationHandler.channelGroup);

await FirebaseMessaging.instance
    .setForegroundNotificationPresentationOptions(
  alert: true,
  badge: true,
  sound: true,
);`

// 添加“某些操作”操作

`final someAction = AndroidNotificationAction(
  'some_action', // Unique identifier for the action
  'some action', // Action label
);

final androidNotificationDetails = AndroidNotificationDetails(
  channelId, // Use fixed channelId for grouping
  'Grouped Notifications', // Use a fixed channel name for grouping notifications
  channelDescription: 'Notification description',
  importance: Importance.max,
  priority: Priority.high,
  icon: '@drawable/notification',
  styleInformation: bigTextStyleInformation,
  visibility: NotificationVisibility.public,
  subText: ''subtext,
  groupKey:
      'groupKey', // Use the groupKey for grouping notifications
  setAsGroupSummary: false, // Not setting this as the summary notification
  actions: [someAction],
  groupAlertBehavior: GroupAlertBehavior.summary,
  largeIcon: DrawableResourceAndroidBitmap(androidIcon),
);

`

`final iOSNotificationDetails = DarwinNotificationDetails(
  presentAlert: true,
  presentBadge: true,
  presentSound: true,
  subtitle: 'uniqueSubtitle',
  threadIdentifier: 'uniqueThreadIdentifier',
  interruptionLevel: InterruptionLevel.active,
  attachments: [attachment], //
);

final notificationDetails = NotificationDetails(
  android: androidNotificationDetails,
  iOS: iOSNotificationDetails,
);`

// 显示个人通知

`await flutterLocalNotificationsPlugin.show(
  message.data.hashCode,
  title,
  subtitle,
  notificationDetails,
  payload: message.data.toString(),
);`

// 显示群组摘要通知

`await flutterLocalNotificationsPlugin.show(
  0, // Use a unique ID for the group summary notification
  '',  // Title of the group summary notification (leave it empty)
  '', // Content of the group summary notification (leave it empty)
  NotificationDetails(
    android: AndroidNotificationDetails(
      channelId, // Use the same channelId as individual notifications
      'Grouped Notifications',
      channelDescription: 'Notifications',
      icon: '@drawable/notification',
      importance: Importance.max,
      priority: Priority.high,
      subText: subtext,
      setAsGroupSummary:
          true, // Set this notification as the summary notification
      groupKey:
          groupKey, // Use the groupKey for grouping notifications
      groupAlertBehavior: GroupAlertBehavior
          .summary, // Show all notification content in the group summary
      tag: uniqueTag, // Set a unique tag for each group of notifications
      largeIcon: DrawableResourceAndroidBitmap(androidIcon),
    ),
    iOS: iOSNotificationDetails,
  ),
  payload: '', // Empty payload or customize as per your requirement
);`

// 期待回电事件

  1. 理想情况下,点击操作即可进行回调
  2. 我期待“onDidReceiveNotificationResponse”和“onDidReceiveBackgroundNotificationResponse”,但这些不会在点击操作时被调用。

请指导。

push-notification flutter-local-notification flutter-notification notification-action android-notification-action
1个回答
0
投票

这样做,编辑初始化函数:

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  print(notificationResponse.payload);
}

await flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveNotificationResponse: notificationTapBackground,
  onDidReceiveBackgroundNotificationResponse: notificationTapBackground
);

如需了解更多信息,您可以查看文档:https://pub.dev/packages/flutter_local_notifications#-usage

© www.soinside.com 2019 - 2024. All rights reserved.