Display Custom Data from Firebase Cloud Messaging console to Flutter app?

≯℡__Kan透↙ 提交于 2020-01-16 12:27:49

问题


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?

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.

What I exactly want is like this: When I send a push notification, it display to a custom screen/url_launcher/widget within my app and that screen/url_launcher/widget shows the data I've inputted in FCM console using the KEY and VALUE that I've set when sending the push notification.

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

below is my code:

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();
        }
        ),
          ),
      );
    }
}

回答1:


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");

Output

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

How to fetch url value from above?

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

Output

stackoverflow



来源:https://stackoverflow.com/questions/59608856/display-custom-data-from-firebase-cloud-messaging-console-to-flutter-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!