Device didn't get message from FCM - firebase

I try to use FCM to send notification to my device but when i send it my device got nothing and i don't know did i do anything wrong ?
Plz tell me did i miss something ?
pubspec.yaml :
AndroidManifest.xml :
/android/build.gradle
/android/app/build.gradle

First of all, I think you mixed up screenshots.. but I'll give it a try:
build.gradle (App):
Add
dependencies {
implementation 'com.google.firebase:firebase-messaging:20.2.4'}
to the bottom.
on iOS devices:
You need to add the "Push Notification" Service to your Runnter Signing & Capabilities.
In your Code:
You need to initialise FCM
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_showItemDialog(message);
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
For more information you can visit firebase_messaging installation walkthrough

Related

Receiving notification in flutter with Firebase

Whenever I receive a notification in firebase flutter, the notification is sent, and appears at the status bar, but my onbackgroundmessage handler isn't called or any onMessage functions. Any help is greatly appreciated. I will provide any further information that is necessary.
main.dart
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
I'm not sure about your code, but try this way, which is the way in the FlutterFire docs:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
}
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
//...
});
But remember also that this code not enough to run when the app terminated where you need further work to handle Android and iOS to receive them.
FlutterFire really helps, also the example there: https://firebase.flutter.dev/docs/messaging/overview
Ready to help if it's still not working.
I managed to get the notifications working. What I did was send the notification with only data and no notification.
e.g.
BEFORE:
{notification: {title: 'my title', body: 'my body'}, data: {'click_action': 'FLUTTER_NOTIFICATION_CLICK'}
NEW:
{data: {'click_action': 'FLUTTER_NOTIFICATION_CLICK', 'title': 'my title', 'body': 'my body'}}

Flutter: access SharedPreferences from onBackgroundMessage method of FirebaseMessaging

In my Flutter app I want to save data received from a Firebase Data Message to SharedPreferences.
I have the FirebaseMessaging object configured like this:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
// this works fine
print("onMessage: ");
},
onLaunch: (Map<String, dynamic> data) {
print("onLaunch: ");
},
onResume: (Map<String, dynamic> data) {
print("onResume: ");
},
onBackgroundMessage: myBackgroundMessageHandler,
);
And this is the top-level myBackgroundMessageHandler method:
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
// This doesn't work
print("background message"); // this isn't even printed!
SharedPreferences.getInstance().then(...) // here I have to save some data
}
The problem is that myBackgroundMessageHandler seems not to be called at all.
However, the notification is received on the phone.
I also tryed to remove the notification field from the message and in that case myBackgroundMessageHandler is called but the notification doesn't arrive. Is there a way to both receive the notification and trigger the method?

Flutter Show Notification when app is open with Firebase Messaging

the app receives all the notification that I have sent in the background or closed mode, but I also want to show the notification when the app is opened while user playing with the app.
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
FlutterRingtonePlayer.playNotification();
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
// TODO optional
},
If you want to show the notification in the System tray, as it does when the app is in the background, you can use the package flutter_local_notifications.
This way, when you receive a notification via onMessage, you can use something like this:
AndroidNotificationDetails notificationAndroidSpecifics =
AndroidNotificationDetails(
groupChannelId, groupChannelName, groupChannelDescription,
importance: Importance.Max,
priority: Priority.High,
groupKey: groupKey);
NotificationDetails notificationPlatformSpecifics =
NotificationDetails(notificationAndroidSpecifics, null);
await flutterLocalNotificationsPlugin.show(
1,
'Jeff Chang',
'Please join us to celebrate the...',
notificationPlatformSpecifics);
Check their documentation for more examples!

How to open a link when user clicks on Firebase notification in a Flutter App?

I am using Firebase in a Flutter app. To send notifications, I am using Firebase Messaging plugin. Whenever I send a notification using Firebase website, the notification just opens the app when user clicks on it. I want to send a notification which opens a URL instead of the app.
I don't know if this information is useful: while composing a notification on Firestore, I always put click_action as FLUTTER_NOTIFICATION_CLICK in "Custom data" section in Additional Options.
You can use onLaunch() and onResume() methods to handle notification's opening action.
for more information of notification's reaction on different platforms please see below link:
https://pub.dev/packages/firebase_messaging#receiving-messages
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
And you can use url_launcher to open URLs:
https://pub.dev/packages/url_launcher

How to disable cloud messaging in flutter app?

I am making an app with push notifications. These are already implemented with FirebaseMessaging.
This is the code for it:
_firebaseMessaging = new FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print("Message: " + message.toString());
},
onResume: (Map<String, dynamic> message) {
print("Message: " + message.toString());
},
onLaunch: (Map<String, dynamic> message) {
print("Message: " + message.toString());
},
);
_firebaseMessaging.getToken().then((token) {
print(token);
});
I also have a settings page in the app. There it is possible for the user to uncheck the property for receiving notifications. Under the hood, it's just a variable that is set to false at the moment.
My question is: How is it possible to disable the push notifications for those users?
You can use Firebase topics to only send notifications to devices which subscribed to a topic.
The firebase_messaging also supports topics.

Resources