Flutter Show Notification when app is open with Firebase Messaging - firebase

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!

Related

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?

Device didn't get message from FCM

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

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

Firebase clod messaging in flutter : open specific route when clicking on a notification (get a context)

I'm using firebase_messaging to get push notifications on the app .. I'm sending with the notification a route to display a specific page when clicking on the notification with the help of flutter_local_notification like this:
(I've not a good context)
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
print(message['route']);
globals.firebaseIn(message['route']);
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
globals.firebaseOut(message['route']);
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
globals.firebaseOut(message['route']);
},
);
void firebaseIn(String route) {
showNotificationWithDefaultSound(route);
}
Future onSelectNotification(String payload) async {
router.navigateTo(currentContext, payload,
transition: TransitionType.inFromRight,
transitionDuration: const Duration(milliseconds: 500));
}
Future showNotificationWithDefaultSound(String route) async {
var androidPlatformChannelSpecifics = new
AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'استطلاع جديد',
'لديك استطلاع جديد',
platformChannelSpecifics,
payload: route,
);
}
void firebaseOut(String route) {
router.navigateTo(currentContext, route,
transition: TransitionType.inFromRight,
transitionDuration: const Duration(milliseconds: 500));
}
This works perfectly when the notification is received while the application is open .. but when the application is running on the background or closed and the notification is received, when clicking on the notification the application will open on its state from the background or from the start if it was closed...
I want the specific route to open when clicking on the notification even if it was in the background or closed .. how to do that? is there is a way to achieve this in flutter?

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