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

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

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'}}

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

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 show a push-notification when the application is open(foreground), when OnMessage is triggered?

Im use Flutter and Firebase Messaging.
I im configure Firebase like in example: firebaseMessaging.configure(
onMessage: ...
onLaunch: ...
onResume: ...
)
But i wanna see push-notification even when app is open.
Roughly speaking onMessage should work like onResume. How can i do this?
onMessage: (Map<String, dynamic> message) async {
showNotification(message);
print('on message $message');
}
showNotification(Map<String, dynamic> msg) async {
var android = new AndroidNotificationDetails(
'your channel id',//channel id
"your channel name",//channel name
"your channel description",//channel desc todo set all this right
icon: 'mipmap/launcher_icon'//add your icon here
);
var iOS = new IOSNotificationDetails();
var platform = new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin
.show(0, msg['notification']['title'], msg['notification']['body'], platform);
}
I used flutter_local_notifications: ^1.2.2 to show local notification foreground.
Additionally, if you are implementing for IOS don't forget to ask for notification permission.

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