如何为特定令牌、颤振、firebase 消息传递和 node.js 发送通知
Posted
技术标签:
【中文标题】如何为特定令牌、颤振、firebase 消息传递和 node.js 发送通知【英文标题】:How to send a notification for a specific token, flutter, firebase messaging and node.js 【发布时间】:2021-11-04 03:36:40 【问题描述】:我正在设计一个应用程序,其中我将在 onCreate 事件之后在客户端之间建立一个通知系统。 这是我的 index.js 代码
const functions= require("firebase-functions");
const admin =require("firebase-admin");
admin.initializeApp();
var fcm = admin.messaging();
// Node.js e.g via a Firebase Cloud Function
exports.sendPush = functions.firestore.document('notifications/notificationId').onCreate((change, context)=>
const chauffeur = change.after.data().chauffeur;
const date_reception = change.after.data().date_reception;
const send_name = change.after.data().send_name;
const token = change.after.data().token;
console.log('chauffeur' + chauffeur);
console.log('date_reception' + date_reception);
console.log('send_name' + send_name);
console.log('token' + token);
const payload =
notification:
title: 'New message',
body: 'Message reçu de' + chauffeur,
sound: "default",
,
data:
'chauffeur':chauffeur,
'date_reception': date_reception,
'send_name': send_name,
,
return admin.messaging().sendToDevice(token, payload);
);
et mon code dart&flutter
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
Future<void> _messageHandler(RemoteMessage message) async
print('background message $message.notification!.body');
void main() async
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_messageHandler);
runApp(MessagingTutorial());
class MessagingTutorial extends StatelessWidget
static const String idScreen = "note";
@override
Widget build(BuildContext context)
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Firebase Messaging',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Firebase Messaging'),
);
class MyHomePage extends StatefulWidget
MyHomePage(Key? key, this.title) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
late FirebaseMessaging messaging;
String? notificationText;
@override
void initState()
super.initState();
messaging = FirebaseMessaging.instance;
messaging.getToken().then((value)
print(value);
);
FirebaseMessaging.onMessage.listen((RemoteMessage event)
RemoteNotification? notification = event.notification;
androidNotification? androidNotification = event.notification!.android;
print("message recieved");
print(event.notification!.body);
print(event.data.values);
showDialog(
context: context,
builder: (BuildContext context)
return AlertDialog(
title: Text("Notification $event.data['title']"),
content: Text(event.notification!.body!),
actions: [
Row(children: [
TextButton(
child: Text("Annuler"),
onPressed: ()
Navigator.of(context).pop();
,
),
TextButton(
child: Text("Ok"),
onPressed: ()
Navigator.of(context).pop();
,
)
])
],
);
);
);
FirebaseMessaging.onMessageOpenedApp.listen((message)
print('Message clicked!');
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text(widget.title!),
),
body: Center(child: Text("Messaging Tutorial")),
);
问题是,当我保存在我的“通知”集合中时,通知甚至不会在后台显示,甚至在前台也不显示。
在收藏中保存后,该功能的使用次数增加,但在应用中没有效果。 usage for the function 当我从 firebase 云消息传递测试消息时,一切正常
Cloud messaging test 我不知道如何解决这个问题,如果有人可以帮助我,我会很高兴。 谢谢
【问题讨论】:
【参考方案1】:这里可能有很多失败的点。冷启动可能会导致您没有等待足够的通知。当您的应用程序打开时,如果您尚未为其编写代码来处理焦点时的消息,它将不会显示任何内容。您可能有一个过时的通知令牌。你会在你的数据库上更新它吗?
你能用payload
的这个shema试试吗:
const payload =
notification:
title: "title",
body: "body",
,
webpush:
notification:
title: "title",
body: "body",
,
,
data:
test: 'test',
,
【讨论】:
以上是关于如何为特定令牌、颤振、firebase 消息传递和 node.js 发送通知的主要内容,如果未能解决你的问题,请参考以下文章