英文:
Display Custom Data from Firebase Cloud Messaging console to Flutter app?
问题
Sure, here's the translated content from your provided text:
"嗨,有没有办法在我的Flutter应用程序中使用我在Firebase Cloud Messaging控制台中设置的“key”和“value”来在推送通知中显示?实际上,我很难使这个工作,例如,我在我的FCM控制台中使用了一个“url”作为键,一个链接作为值。
我确切想要的是这样的:当我发送推送通知时,它会显示在我的应用程序内的自定义屏幕/url_launcher/widget上,并且该屏幕/url_launcher/widget会显示我在发送推送通知时在FCM控制台中输入的数据,使用我设置的键和值。
问题是如何在我的应用程序中显示这些数据?如何使用这些键和值?实际上,我对如何编写它有点迷茫。
以下是我的代码:"
Please note that the code portion remains in English, as requested.
英文:
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
得分: 4
这是您应该从控制台发送自定义数据的方式,
您可以像这样接收通知,
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("$message");
输出
{"notification": {"title": "rrakkk", "body": "wer"}, "data": {"url": "stackoverflow"}}
如何从上面获取 url
值?
print("${message['data']['url']}");
输出
stackoverflow
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论