How to send a notification for a specific token, flutter, firebase messaging and node.js
There could be a lot of points that fail here. The cold start could cause that you don't wait enought for the notification. When your App is open it won't show anything if you havend written code for it to handle messages while in focus. You could have an outdated notification token. Do you update it on your database?
Can you try it with this shema of the payload
to:
const payload = {
notification: {
title: "title",
body: "body",
},
webpush: {
notification: {
title: "title",
body: "body",
},
},
data: {
test: 'test',
},
}
Frank van Puffelen
I am an engineer for Firebase at Google. I respond equally well to being called "Frank" or "puf".
Updated on January 01, 2023Comments
-
Frank van Puffelen 10 months
I am designing an application and in it I will have a notification system between clients after an onCreate event. Here is my index.js code
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")), ); } }
The problem is, when I save in my "notifications" collection, the notification doesn't show even in the background, not even in forground.
After saving in the collection, the number of uses of the function increases but no effect in the application. usage for the function When I send the test message from firebase cloud messaging, everything is working fine
Cloud messaging test I don't know how to fix this, if anyone can help me i would be very happy. Thank you