- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在此代码上的应用程序内(在主屏幕中)创建通知徽章,并且我正在使用 Firebase Cloud Messaging 和 android 应用程序以 flutter 语言,问题是我无法弄清楚如何计算收到的通知数量所以
有什么建议的方法来计算从 Firebase 云消息传递到 android 应用程序接收到的通知数量吗?
PS:我现在已经为下面的答案更新了代码,但我仍然遇到错误
// import 'package:flutter/foundation.dart';
// import 'package:flappy_search_bar/flappy_search_bar.dart';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:mycafe/main.dart';
import 'Custom_Text.dart';
import 'Pasta.dart';
import 'Burger.dart';
import 'Pizza.dart';
import 'AboutUs.dart';
import 'dart:async';
import 'ui/home/HomeScreen.dart';
import 'dart:math';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'ContactUs.dart';
import 'package:flutter/services.dart';
import 'package:mycafe/model/User.dart';
import 'package:mycafe/ui/home/HomeScreen.dart';
import 'package:mycafe/ui/services/Authenticate.dart';
import 'package:mycafe/ui/utils/helper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';
import 'dart:io';
import 'package:flushbar/flushbar.dart';
import 'package:flushbar/flushbar_helper.dart';
// import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'constants.dart' as Constants;
import 'ui/auth/AuthScreen.dart';
import 'ui/onBoarding/OnBoardingScreen.dart';
import 'package:flutter/cupertino.dart';
import 'package:mycafe/ui/auth/AuthScreen.dart';
var bannerItems = ["Burger", "cheesechilly", "Noodles", "Pizza"];
var bannerImages = [
"images/burger.jpg",
"images/cheesechilly.jpg",
"images/noodles.jpg",
"images/pizza.jpg"
];
ValueNotifier<int> notificationCounterValueNotifer = ValueNotifier(0);
class Notify extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'FlutterBase',
home: Scaffold(
body: MessageHandler(),
),
);
}
}
class MessageHandler extends StatefulWidget {
@override
_MessageHandlerState createState() => _MessageHandlerState();
}
class _MessageHandlerState extends State<MessageHandler> with ChangeNotifier {
final Firestore _db = Firestore.instance;
final FirebaseMessaging _fcm = FirebaseMessaging();
StreamSubscription iosSubscription;
@override
void initState() {
super.initState();
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
print(data);
_saveDeviceToken();
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
// void _incrementCounter() {
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
// RaisedButton(
// child: Text(message['notification']['title']),
// onPressed: () {
// Flushbar(
// flushbarPosition: FlushbarPosition.TOP,
// icon: Icon(
// Icons.notifications_active,
// color: Colors.white,
// ),
// mainButton: FlatButton(
// onPressed: () {
// Navigator.pop(context);
// // Flush.showGoodFlushbar(context, 'login successful!');
// },
// // child: Text(
// // "ADD",
// // style: TextStyle(color: Colors.amber),
// // ),
// ),
// // duration: Duration(seconds: 7))
// ).show(context);
// },
// );
notificationCounterValueNotifer.value++;
notificationCounterValueNotifer
.notifyListeners(); // notify listeners here so ValueListenableBuilder will build the widget.
final snackbar = SnackBar(
content: Text(message['notification']['title']),
action: SnackBarAction(
label: 'Go',
onPressed: () => null,
),
);
Scaffold.of(context).showSnackBar(snackbar);
// showDialog(
// context: context,
// builder: (context) => AlertDialog(
// content: ListTile(
// title: Text(message['notification']['title']),
// subtitle: Text(message['notification']['body']),
// ),
// actions: <Widget>[
// FlatButton(
// color: Colors.amber,
// child: Text('Ok'),
// onPressed: () => Navigator.of(context).pop(),
// ),
// ],
// ),
// );
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
// TODO optional
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
// TODO optional
},
);
}
@override
void dispose() {
if (iosSubscription != null) iosSubscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
// _handleMessages(context);
return MaterialApp(home: Scaffold(body: HomeApp()));
}
/// Get the token, save it to the database for current user
_saveDeviceToken() async {
// Get the current user
String uid = 'jeffd23';
// FirebaseUser user = await _auth.currentUser();
// Get the token for this device
String fcmToken = await _fcm.getToken();
// Save it to Firestore
if (fcmToken != null) {
var tokens = _db
.collection('users')
.document(uid)
.collection('tokens')
.document(fcmToken);
await tokens.setData({
'token': fcmToken,
'createdAt': FieldValue.serverTimestamp(), // optional
'platform': Platform.operatingSystem // optional
});
}
}
/// Subscribe the user to a topic
_subscribeToTopic() async {
// Subscribe the user to a topic
_fcm.subscribeToTopic('puppies');
}
}
Widget myAppBarIcon() {
if (State is ValueNotifier) {
return ValueListenableBuilder(
builder: (BuildContext context, int newNotificationCounterValue,
Widget child) {
// return Container(
// width: 50,
// height: 10,
child:
Stack(
children: [
Icon(
Icons.notifications,
color: Colors.white,
size: 30,
),
Container(
width: 30,
height: 30,
alignment: Alignment.topRight,
margin: EdgeInsets.only(top: 2),
child: Container(
width: 15,
height: 15,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffc32c37),
border: Border.all(color: Colors.white, width: 1)),
child: Padding(
padding: const EdgeInsets.all(0.0),
child: Text(
newNotificationCounterValue.toString(),
),
),
),
),
],
);
//return your badge here
},
valueListenable: notificationCounterValueNotifer,
);
// return Container(
// width: 50,
// height: 10,
// child: Stack(
// children: [
// Icon(
// Icons.notifications,
// color: Colors.white,
// size: 30,
// ),
// Container(
// width: 30,
// height: 30,
// alignment: Alignment.topRight,
// margin: EdgeInsets.only(top: 2),
// child: Container(
// width: 15,
// height: 15,
// decoration: BoxDecoration(
// shape: BoxShape.circle,
// color: Color(0xffc32c37),
// border: Border.all(color: Colors.white, width: 1)),
// child: Padding(
// padding: const EdgeInsets.all(0.0),
// child: Center(
// child: ValueListenableBuilder(),
// ),
// ),
// ),
// ),
// ],
// ),
// );
} else {
return Container();
}
}
class HomeApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepOrange[900],
title: Text('HuQQa BuzZ'),
actions: <Widget>[myAppBarIcon()],
最佳答案
我就是这样做的:
//define a global variable
ValueNotifier<int> notificationCounterValueNotifer =
ValueNotifier(0);
您可以阅读
ValueNotifier
here .
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
notificationCounterValueNotifer.value++;
notificationCounterValueNotifer.notifyListeners(); // notify listeners here so ValueListenableBuilder will build the widget.
// final snackbar = SnackBar(
// content: Text(message['notification']['title']),
// action: SnackBarAction(
// label: 'Go',
// onPressed: () => null,
// ),
// );
// Scaffold.of(context).showSnackBar(snackbar);
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
color: Colors.amber,
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
},
);
为了通知听众,您必须添加
ChangeNotifer
作为你类(class)的混入:
class _MessageHandlerState extends State<MessageHandler> with ChangeNotifier
您可以选择
ValueListenableBuilder每当值更改时构建小部件。所以要更新徽章,我们使用这个小部件:
ValueListenableBuilder(
builder: (BuildContext context, int newNotificationCounterValue, Widget child) {
// This builder will only get called when the notificationCounterValueNotifer is updated.
return Text(newNotificationCounterValue.toString()); //return your badge here
},
valueListenable: notificationCounterValueNotifer,
);
您可以将值存储在数据库文档中并在应用程序开始时分配它,以便您的
notificationCounterValueNotifer
值不为 0。
notificationScreen
时再次将其设置为 0。 .
ValueListenableBuilder
来自
myAppBarIcon
:
Widget myAppBarIcon() {
//you have to return the widget from builder method.
//you can add logics inside your builder method. for example, if you don't want to show a badge when the value is 0.
return ValueListenableBuilder(
builder: (BuildContext context, int newNotificationCounterValue,
Widget child) {
//returns an empty container if the value is 0 and returns the Stack otherwise
return newNotificationCounterValue == 0? Container(): Stack(
children: [
Icon(
Icons.notifications,
color: Colors.white,
size: 30,
),
Container(
width: 30,
height: 30,
alignment: Alignment.topRight,
margin: EdgeInsets.only(top: 2),
child: Container(
width: 15,
height: 15,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xffc32c37),
border: Border.all(color: Colors.white, width: 1)),
child: Padding(
padding: const EdgeInsets.all(0.0),
child: Text(
newNotificationCounterValue.toString(),
),
),
),
),
],
);
},
valueListenable: notificationCounterValueNotifer,
);
}
关于android - flutter :FCM 计数通知数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810186/
我正在尝试使用 Firebase 云消息传递。我将通知从 Node.js 服务器发送到我注册到通知系统的应用程序。 我的问题是,在 Android 5.1 上,即使我在 nofitification
根据FCM documentation如果 FCM 服务器检测到高优先级消息不会导致用户交互的模式,则高优先级消息可能会被取消优先级。该机制的细节未指定。问题: 这究竟是如何工作的? 检测算法如何收集
更新到最新的 Firebase 后,无法获取所选 的 FCM 注册 token fcmSenderId : FirebaseInstanceId .getInstance()
我想在单个 fcm 请求中向多个设备发送通知。我的通知文本对于所有设备都是相同的。我必须同时向所有用户发送超过 10000 条通知,并且文本是相同的,所以我想以最小的 fcm 请求发送所有通知。我正在
// body 就像这样 { "to": "/topics/NEWS" , "data":{ "extra_information": "This is
当我在浏览器中生成 FCM token 时,我还将其发送到我的服务器,服务器使用 firebase 管理模块将其订阅到主题,如下所示: messaging.subscribeToTopic(token
我正在开发一个应用程序,我想通过 php 实现 FCM 推送通知。 所以我制作了两个java文件:1.FirebaseInstanceID(工作正常并在数据库中正确获取 token )2.Fireba
我正在尝试使用 cordova-plugin-firebasex 向我的cordova 应用程序上的特定用户发送通知这是 cordova-plugin-firebase 的一个 fork 修复和改进。
我正在尝试使用以下方法从我的 iOS-Objective-C 应用发送消息: NSInteger iTime = [NSDate timeIntervalSinceReferenceDate]
我正在尝试为 FCM 实现服务器以向 android/iOS 设备发送通知。 我需要向 android 和 iOS 发送纯数据通知,但 iOS 的后台通知似乎非常不稳定。 (即使该应用程序在前台,我也
我对向手机发送通知的选项感到困惑。我在 Azure 上部署了在 .Net core 中创建的后端。现在,当管理员推出新优惠时,必须将推送通知发送到移动设备。我找到了 3 个很好的替代品。所有人都以自己
我用 POST 设置了一个 REST 客户端 --> https://fcm.googleapis.com/fcm/send 内容类型:应用程序/json 授权: key = JSON 正文: {
我在 Flutter 应用程序中使用 firebase_messaging v9.0.1。关于基于 https://pub.dev/packages/firebase_messaging/exampl
当我使用 Firebase 控制台时,通过激活“高级选项”下的“声音”一切都是完美的,正如 Mouad Abdelghafour AitAli 在接受的答案中所解释的那样 Firebase Push
我已经尝试在 Curl 中使用以下命令使用 Firebase REST Api 发送通知并且它有效: curl -X POST --header "Authorization: key=AIza...
即使 ios 也可以从 fcm 控制台获得通知。 Controller 功能 : public function push(Request $request) { $validator = V
对于通过 XMMP 支持上行和下行消息的服务器端实现,我使用 org.jivesoftware.smack.tcp.XMPPTCPConnection。我从 GCM 迁移到 FCM,现在我的应用服务器
这是我的代码: function sendToken(token) { $.ajax({ url: 'https://iid.googleapis.com/iid/v1/' + token
这是我第一次使用 Flutter 测试 FCM。我检查了一些来自 GitHub 的 SO 问题和文档。 我能够发送通知,当应用程序未运行时它们会被发送。 如果应用程序正在运行或在后台,则消息不可见。
我正在尝试在未接收但他们在调试 apk 中发送/接收很好的发布应用程序中发送 FCM 通知(在用户之间发送消息、好友请求等),我有对此进行了搜索,发现一些对我不起作用的解决方案 像这样,我把它放在 p
我是一名优秀的程序员,十分优秀!