Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

Firebase Messaging can't send background messages Flutter

I have got the following code in my index.ts

const payload: admin.messaging.MessagingPayload = {
      notification: {
        title: 'New Order!',
        body: you sold a ${order.day} for ${order.time},
      },
    }; 
    return fcm.sendToDevice(tokens, payload);

And this in my Flutter:

 messaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
      onBackgroundMessage: myBackgroundMessageHandler
    );
  }
  static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    var data = message['data'] ?? message;
    print(data);
    // Or do other work.
  }

While the onMessage part works perfectly finely, onLaunch and onResume don't work. Netierh does the onBackgroundMessage.

question from:https://stackoverflow.com/questions/65942443/firebase-messaging-cant-send-background-messages-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use onBackgroundMessage method and flutter_local_notifications package https://pub.dev/packages/flutter_local_notifications enter image description here

you are checking data not notification

 static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
    var data = message['notification'] ?? message;
    print(data);
    // Or do other work.
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...