Flutter Flatter Firebase消息触发器onLaunch

Flutter Flatter Firebase消息触发器onLaunch,flutter,firebase-cloud-messaging,Flutter,Firebase Cloud Messaging,所以我有一个问题,因为我没有完全理解一些东西。所以我写了一个简单的应用程序,尝试一些东西,所以我通过Firebase云消息从Firebase发送消息。我使用firebase_消息传递颤振插件(7.0.3版本)。它只是做了一件简单的事情来获取消息并导航到另一个页面。我的问题是,onMessage和onResume功能工作得很好,但当应用程序终止,我点击通知时,应用程序打开,但什么也没有发生。所以我阅读了文档,发现了这个 第三行说,数据丢失了。。。这是否意味着我丢失了有效载荷中的所有数据? 如果有帮

所以我有一个问题,因为我没有完全理解一些东西。所以我写了一个简单的应用程序,尝试一些东西,所以我通过Firebase云消息从Firebase发送消息。我使用firebase_消息传递颤振插件(7.0.3版本)。它只是做了一件简单的事情来获取消息并导航到另一个页面。我的问题是,onMessage和onResume功能工作得很好,但当应用程序终止,我点击通知时,应用程序打开,但什么也没有发生。所以我阅读了文档,发现了这个 第三行说,数据丢失了。。。这是否意味着我丢失了有效载荷中的所有数据? 如果有帮助,我的代码在这里

 static final NavigationService _navigationService =
      loc.Locator.locator<NavigationService>();
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  Future<void> reqPermission() async {
    bool permission = true;
    if (Platform.isIOS) {
      permission = await _firebaseMessaging.requestNotificationPermissions();
    }
    if (permission) {
      _firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) async {
          print(message);
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onLaunch: (Map<String, dynamic> message) async {
          print('onLaunch $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onResume: (Map<String, dynamic> message) async {
          print('onResume $message');
          await _navigationService.navigateTo(NewPage.routeName);
        },
        onBackgroundMessage: _myBackgroundHandler,
      );
    }
  }
静态最终导航服务\u导航服务=
loc.Locator.Locator();
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
Future reqPermission()异步{
bool permission=true;
if(Platform.isIOS){
permission=wait_firebaseMessaging.requestNotificationPermissions();
}
如果(许可){
_firebaseMessaging.configure(
onMessage:(映射消息)异步{
打印(信息);
wait_navigationService.navigateTo(NewPage.routeName);
},
onLaunch:(映射消息)异步{
打印('onLaunch$message');
wait_navigationService.navigateTo(NewPage.routeName);
},
onResume:(映射消息)异步{
打印('onResume$message');
wait_navigationService.navigateTo(NewPage.routeName);
},
onBackgroundMessage:_myBackgroundHandler,
);
}
}

您可以使用fcm发送通知和数据。fcm支持终止通知,但不支持数据。您可以在通知中发送正文和标题,所以如果您需要基本通知(只有标题和正文)。您可以使用通知发送它。 如果您需要更多信息,可以将其与flatter\u local\u通知包一起使用 在onLaunch。

这就是如何使用代币

_firebaseMessaging.onTokenRefresh.listen((newToken) {
      User _currentUser = FirebaseAuth.instance.currentUser;
      FirebaseFirestore.instance
          .doc("tokens/" + _currentUser.uid)
          .set({"token": newToken});
    });
你可以这样推它

  Future<bool> sendNotification(
      {@required Map<String, dynamic> messageMap,
      @required AppUser appUser,
      @required String token}) async {
    String url = "https://fcm.googleapis.com/fcm/send";
    String _firebaseKey ="<your key>"

    Map<String, String> headers = {
      "Content-type": "application/json",
      "Authorization": "key=$_firebaseKey"
    };
    String json =
        '{ "to" : "$token", "data" : { "message" : "${messageMap["message"]}", "sendBy": "${appUser.name}", "messageType": "${messageMap["messageType"]}", "sendById" : "${appUser.userId}" } }';
    http.post(url, headers: headers, body: json);
    return true;
  }
未来发送通知(
{@required-Map-messageMap,
@必需的AppUser AppUser,
@必需的字符串标记})异步{
字符串url=”https://fcm.googleapis.com/fcm/send";
字符串_firebaseKey=“”
映射头={
“内容类型”:“应用程序/json”,
“授权”:“密钥=$\u firebaseKey”
};
字符串json=
“{”to:“$token”,“data:“{”message:“${messageMap[“message”]}”,“sendBy:“${appUser.name}”,“messageType:“${messageMap[“messageType”]}”,“sendById:“${appUser.userId}}”;
post(url,headers:headers,body:json);
返回true;
}
FCM配置

_firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        //getNotificationMessage(message["data"]["message"]);
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message); // Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onResume: (Map<String, dynamic> message) async {
        if(message["data"]["messageType"]=="text"){
          NotificationHandler.showNotification(message);// Flutter Local Notification method
        } else{
          NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
        }
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );
  }
\u firebaseMessaging.configure(
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
//getNotificationMessage(消息[“数据”][“消息”]);
如果(消息[“数据”][“消息类型”]=“文本”){
NotificationHandler.showNotification(消息);//颤振本地通知方法
}否则{
NotificationHandler.showBigPictureNotification(消息);//颤振本地通知方法
}
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
如果(消息[“数据”][“消息类型”]=“文本”){
NotificationHandler.showNotification(消息);//颤振本地通知方法
}否则{
NotificationHandler.showBigPictureNotification(消息);//颤振本地通知方法
}
},
onResume:(映射消息)异步{
如果(消息[“数据”][“消息类型”]=“文本”){
NotificationHandler.showNotification(消息);//颤振本地通知方法
}否则{
NotificationHandler.showBigPictureNotification(消息);//颤振本地通知方法
}
},
onBackgroundMessage:myBackgroundMessageHandler,
);
}
这是后台消息处理程序

Future<void> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  if (message.containsKey('data')) {
    // Handle data message
    final dynamic data = message['data'];
    if(message["data"]["messageType"]=="text"){
      NotificationHandler.showNotification(message);// Flutter Local Notification method
    } else{
      NotificationHandler.showBigPictureNotification(message);// Flutter Local Notification method
    }

  }

  return Future.value();
}
未来myBackgroundMessageHandler(映射消息)异步{ if(message.containsKey('data')){ //处理数据消息 最终动态数据=消息['data']; 如果(消息[“数据”][“消息类型”]=“文本”){ NotificationHandler.showNotification(消息);//颤振本地通知方法 }否则{ NotificationHandler.showBigPictureNotification(消息);//颤振本地通知方法 } } 返回Future.value(); }
这就是我在项目中如何做到的

我最近在GitHub上问了这个问题,请参见:

旧版本也是如此,它需要一个本地解决方案。最新的非nullsafety版本firebase_消息:^8.0.0-dev.14将在终止状态下工作(除非您在android上强制退出该应用)。更新的文档位于


我想在数据部分发送路由的名称。这是可能的还是不可能的?@Butch,这是可能的。我在回答中添加了一个例子。也许对你有用