簡體   English   中英

flutter_local_notifications onNotificationReceived 方法 - Flutter

[英]flutter_local_notifications onNotificationReceived method - Flutter

閱讀時間過長:問題

基本上,我正在尋找一個onMessageReceived()回調,但它適用於flutter_local_notifications,因此我可以處理數據並將其顯示給用戶。 這個插件只支持處理onNotificationTap()動作。

  • 當用戶收到消息時,例如,如果他們啟用了Do Not Disturb ”,我應該如何處理該消息? 即使本地通知沒有顯示,我至少需要顯示一個覆蓋,由一些onMessageReceived()函數觸發。
  • 收到(預定)本地通知時,如何更新數據庫中的 notificationCount?

描述

在我的項目中,我正在使用:

  1. Firebase 雲消息傳遞 (FCM)
  2. flutter_local_notifications包。

在我的應用程序中安排事件時,過程如下:

  1. 使用僅數據消息向 FCM 發出POST請求。
  2. 我的應用通過onMessageReceived()回調接收消息。
  3. 幾乎立即我得到'Got a message whilst in the foreground!' 信息。 這是由即時 FCM 數據消息觸發的。
  4. 消息中的數據會觸發flutter_local_notifications來安排通知。
  5. 此計划的本地通知在以后收到,無法處理(沒有 OnMessage() 函數)。

我沒有直接在 FCM 上安排通知,因為你不能從發布請求(奇怪)中做到這一點,但這會解決我所有的問題。

問題

  • 當通知到達用戶的設備時,無法處理消息(前台或后台)
  • 如果用戶在前台,我無法在通知中顯示Overlay
  • 我無法自動更新 Firebase 實時數據庫中的notificationCount

基本上,我正在尋找一個onMessageReceived()回調,但它適用於flutter_local_notifications,因此我可以處理數據並將其顯示給用戶。 此插件僅支持處理 onNotificationTap()` 動作。

我的過程示例

這就是 FCM 所具有的而flutter_local_notifications沒有的。 當我的應用收到通知時觸發:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('Got a message whilst in the foreground!');

[...]

if (scheduledDate != null) {
//using flutter_local_notifications
  sendScheduledLocalNotification(itemTitle, 'Due Today', formattedDate);
//this notification, received at a later date, cannot be processed with this same function because it doesn't use FCM
}

//only shown with instant notifications (not scheduled)
if (notification != null) {
  showOverlayNotification((context) {
    return LocalNotificationOverlay(
      title: notification.title!,
      subtitle: notification.body!,
      imageUrl: notification.imageUrl!,
    );
  }, duration: Duration(seconds: 3));
}
  }

});

是的,據我所知,我們沒有與 FCM 相同的流選項來收聽系統托盤中的flutter_local_notifications通知。

你可以檢查這個答案,它可能對你有幫助。

我想最后你最好在后端進行,並且再次通過 FCM 發送/接收這個預定的消息。

我不知道您正在使用的機制以及向用戶發送通知的功能,但如果它有關於調度的信息,您可以忽略向用戶發送通知。 根據您的代碼, scheduledDateformattedDate知道它。 因此,在您發送通知的雲功能中,您可以代替發送通知、執行某些任務或觸發某些事情(類似於附加的答案),這將根據時間安排發送該通知。 然后在前端,例如,您可以在每次收到 FCM 通知時更新您的數據庫,因為您一開始並沒有發送它們,而是您實際上安排在計划的時間發送它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM