从Firebase Cloud Messaging控制台显示自定义数据到Flutter应用程序? [英] Display Custom Data from Firebase Cloud Messaging console to Flutter app?

查看:117
本文介绍了从Firebase Cloud Messaging控制台显示自定义数据到Flutter应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,有什么方法可以使用我在Firebase Cloud Messaging控制台中设置的keyvalueAdditional Options推送通知到Flutter应用程序中的DISPLAY吗?

Hi is there any way to use the key and value that I've set in my Firebase Cloud Messaging console, Additional Options for push notification to DISPLAY inside my Flutter app?

我很难完成这项工作,例如,我在FCM控制台中使用url作为键,并使用一个链接作为我的值.

I'm having a hard time making this work tbh, Example, I've used a url for key and a link for my value in my FCM console.

我真正想要的是这样的:当我发送推送通知时,它会显示到我的应用程序中的自定义屏幕/url_launcher/widget,并且该屏幕/url_launcher/widget会显示我使用FCM控制台输入的数据发送推送通知时设置的 KEY VALUE .

问题是如何在我的应用程序中显示此数据?我该如何使用这些键和值? 我有点不知道如何编码

The problem is how do I display this data in my app? how do I use those key and value? I'm kinda lost with how to code it tbh

下面是我的代码:

import 'dart:async';
import 'dart:io';
import 'dart:math';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {

  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();


@override
void initState() {
  super.initState();
  firebaseCloudMessagingListeners();
}
void firebaseCloudMessagingListeners() {
  if (Platform.isIOS) iOSPermission();

  _firebaseMessaging.getToken().then((token){
    print(token);
  });

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      print('on message $message');

    },
    onResume: (Map<String, dynamic> message) async {
      print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) async {
      print('on launch $message');
    },
  );
}

void iOSPermission() {
  _firebaseMessaging.requestNotificationPermissions(
      IosNotificationSettings(sound: true, badge: true, alert: true)
  );
  _firebaseMessaging.onIosSettingsRegistered
      .listen((IosNotificationSettings settings)
  {
    print("Settings registered: $settings");
  });
}



  WebViewController _myController;
      final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  @override
  Widget build(BuildContext context) {
    return SafeArea(
            child: Scaffold(
                  body: WebView(
                  initialUrl: 'https://syncshop.online/en/',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (controller) {
                  _controller.complete(controller);
                },
          onPageFinished: (controller) async {
          (await _controller.future).evaluateJavascript("document.getElementsByClassName('footer-container')[0].style.display='none';");
            (await _controller.future).evaluateJavascript("document.getElementById('st_notification_1').style.display='none';");
            (await _controller.future).evaluateJavascript("document.getElementById('sidebar_box').style.display='none';");
          },
          ),
    floatingActionButton: FutureBuilder<WebViewController>(
        future: _controller.future,
        builder: (BuildContext context, AsyncSnapshot<WebViewController> controller) {
          if (controller.hasData) {
            return FloatingActionButton(
            onPressed: () {
              controller.data.reload();
            },
            child: Icon(Icons.refresh),
          );
          }
          return Container();
        }
        ),
          ),
      );
    }
}

推荐答案

这是从控制台发送自定义数据的方式,

This is how you should send Custom data from the console,

您可以收到这样的通知,

You can receive the notification like this,

   _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("$message");

输出

{通知:{标题:rrakkk,正文:wer},数据:{url:stackoverflow}}

{notification: {title: rrakkk, body: wer}, data: {url: stackoverflow}}

如何从上方获取url值?

How to fetch url value from above?

 print("${message['data']['url']}");

输出

stackoverflow

stackoverflow

这篇关于从Firebase Cloud Messaging控制台显示自定义数据到Flutter应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆