onBackgroundMessage 没有被调用 flutter 消息传递

cus*_*pps 5 flutter firebase-cloud-messaging

firebase messaging我在我的 flutter 应用程序上获得了令人沮丧的体验onBackgroundmessage,因为发送请求后应用程序处于后台时没有被触发。我想知道为什么,因为这是我的应用程序需要的核心功能。

  Future initialize(context) async{
    _firebaseMessaging.configure(
   
    onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,

    
    );
  }
  Future<dynamic> _myBackgroundMessageHandler
      (Map<String, dynamic> message) async {
      print("onBackground Message called");
    fetchRideInfoInBackground();
    return Future<void>.value();
  }

}
Run Code Online (Sandbox Code Playgroud)

通知负载

const message = {
      notification: {
        title: 'New Incoming Request',
        body: `New incoming ${service} request`,
      },
      data: { orderId: orderId },
      android: {
        ttl: 30,
        priority: 'high',
        notification: {
          click_action: 'FLUTTER_NOTIFICATION_CLICK',
        },
      },
      tokens: driversRegistrationToken,
    };
Run Code Online (Sandbox Code Playgroud)

Dán*_*zsa 0

我没有看到整个文件,但我假设你的_myBackgroundMessageHandler不是顶级函数。
使其成为顶级或静态,之后它应该可以工作。

编辑:示例


Future<dynamic> _myBackgroundMessageHandler (Map<String, dynamic> message) async {
    print("onBackground Message called");
    fetchRideInfoInBackground();
    return Future<void>.value();
}

 class PushHandler {
 
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Future initialize(context) async{
    _firebaseMessaging.configure(
      onBackgroundMessage: Platform.isIOS ? null:_myBackgroundMessageHandler,
    );
  }
 }
Run Code Online (Sandbox Code Playgroud)

  • 兄弟还没工作吗?我也把它做成了顶级。你想让我展示完整的代码吗? (2认同)