How can I set the onDidReceiveBackgroundNotificationResponse property of FlutterLocalNotificationsPlugin.initialize?

huangapple go评论105阅读模式
英文:

How can I set the onDidReceiveBackgroundNotificationResponse property of FlutterLocalNotificationsPlugin.initialize?

问题

我正在尝试使用flutter_local_notification来实现本地推送通知,并且希望在Flutter应用程序在后台运行时调用FlutterLocalNotificationsPlugin.show时执行某些操作。我发现FlutterLocalNotificationsPlugin.initialize的onDidReceiveBackgroundNotificationResponse属性是应该编写此类操作的部分。然而,即使我编写以下类似的代码,它似乎也不会被执行。

如何触发onDidReceiveBackgroundNotificationResponse?如果有人知道的话,我将非常感激提供一些建议。

我假设您是指的是Android设备。
此外,即使在运行以下示例代码时,debugPrint也不会被执行。

flutterLocalNotificationsPlugin.initialize(
  initializationSettings,
  onDidReceiveNotificationResponse: (notificationResponse) async {
    debugPrint('[onDidReceiveNotificationResponse]');
    if (notificationResponse.payload != null) {
      debugPrint('notification payload: ${notificationResponse.payload}');
    }
  },
  onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
/* top-level */
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  debugPrint('[notificationTapBackground()]');
}

-版本-

Flutter: 3.10.6
flutter_local_notification: ^14.1.0

英文:

I'm trying to implement local push notifications using flutter_local_notification, and I want to perform certain actions when FlutterLocalNotificationsPlugin.show is called while the Flutter app is in the background. I found that the onDidReceiveBackgroundNotificationResponse property of FlutterLocalNotificationsPlugin.initialize is the part where such actions should be written. However, even if I write code like the following, it doesn't seem to be executed.

How can I trigger onDidReceiveBackgroundNotificationResponse? I would be incredibly grateful if someone who knows could provide some advice.

I assume you are referring to an Android device.
Also, even when running the following sample code, the debugPrint is not being executed.

    flutterLocalNotificationsPlugin.initialize(
      initializationSettings,
      onDidReceiveNotificationResponse: (notificationResponse) async {
        debugPrint('[onDidReceiveNotificationResponse]');
        if (notificationResponse.payload != null) {
          debugPrint('notification payload: ${notificationResponse.payload}');
        }
      },
      onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
    );
/* top-level */
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
  debugPrint('[notificationTapBackground()]');
}

-version-

Flutter: 3.10.6
flutter_local_notification: ^14.1.0

答案1

得分: 1

  • FirebaseMessaging.onMessage.listen 将在收到通知时运行。
  • FirebaseMessaging.instance.getInitialMessage() 将在应用程序仍在后台运行时点击通知时运行。
  • FirebaseMessaging.onMessageOpenedApp.listen 将在应用程序被终止时点击通知时运行。

对于本地通知

  • 当应用程序在后台或终止时点击通知时,将运行 onDidReceiveBackgroundNotificationResponse。
  • 当应用程序在前台时点击通知时,将运行 onDidReceiveNotificationResponse。
  • 对于您的问题,您需要点击通知来运行 debugPrint。
英文:

for firebase notification

  • FirebaseMessaging.onMessage.listen will run when you receive a notification.
  • FirebaseMessaging.instance.getInitialMessage() will run when you tap the notification while the app is still in the background.
  • FirebaseMessaging.onMessageOpenedApp.listen will run when you tap the notification when the app is being terminated.

for local notification

  • onDidReceiveBackgroundNotificationResponse will run when you tap the notification while the app is in the background or terminated.
  • onDidReceiveNotificationResponse will run when you tap the notification while the app is in the foreground.
  • for your problem, you need tap to notification to run debugPrint

huangapple
  • 本文由 发表于 2023年7月31日 22:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定