Flutter Firebase notification onlunch is not working - android

I am using FCM service in my flutter app for push notification. onMessage and onResume are working, the problem is when I remove my app from the recent apps list. I successfully get a push notification when my app is killed but the problem is when I click on it I am not getting the access of the data payload in my flutter code. callerInfo is not being set.
Notifications are not getting delivered when the app is swiped from the background(kill mode).
JSON file
{
"notification": {
"title": "bla bla",
"body" : "bla bla",
"icon":"incoming_call",
"color": "#008080",
"click_action":"FLUTTER_NOTIFICATION_CLICK",
"tag" : "call"
},
"data": {
"caller":"caller1",
"image":"
},
"to":"#token",
"android":{
"priority":"high"
}
}
flutter config code in the initState() {}
_configureFireBaseListeners(){
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async{
//_setMessage(message);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async{
//_setMessage(message);
print("onLaunch: $message");
setState((){
_isCall=message['data']['status'];
callerInfo = message['data'];
});
},
onResume: (Map<String, dynamic> message) async{
//_setMessage(message);
print("onResume: $message");
setState((){
_isCall=message['data']['status'];
callerInfo = message['data'];
});
},
);
}

Related

New FireMessagin version

I have updated my flutter project to the new firebase versions.
And I have encountered the following problem.
The method 'configure' isn't defined for the class 'FirebaseMessaging'.
FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
...
},
onResume: (Map<String, dynamic> message) async {
...
},
onLaunch: (Map<String, dynamic> message) async {
...
},
);
How would you do with the new version?
Its old now : you can use this as per your requirement.
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
});
check this link : https://firebase.flutter.dev/docs/messaging/usage/

Flutter app on android does not open app when notification is clicked

Following instructions on https://pub.dev/packages/firebase_messaging, I have implemented FCM. Although I have set click_action: FLUTTER_NOTIFICATION_CLICK, when I click on the notification tray item, it does not open the app. The data received by the flutter app from the cloud function is as follows. How do I at least open the app upon notification click?
onMessage: {notification: {title: pi, body: text message.},
data: {USER_NAME: pi, click_action: FLUTTER_NOTIFICATION_CLICK, }}
Cloud function payload
const payload = {
notification: {
title: sender,
body: content,
clickAction: 'ChatActivity',
},
data: {
"click_action": 'FLUTTER_NOTIFICATION_CLICK',
"USER_NAME": sender,
}
};
I have also created the notification channels and AndroidManifest has been correctly configured.
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
String description = getString(R.string.notification_channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
I have put the click action to the notification json and it works for me.
notification: {
title: sender,
body: content,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
The click action is only required for onResume and onLaunch callbacks, so be sure to implement it as stated in the pub.dev doc:
_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);
},
);

Flutter - Call Firebase onMessage method when the app is in background

The question is same as here Call onMessage method when the app is in background in flutter and I am following the thread but since there is no response, I am asking this question again.
I've connected my Flutter app with Firebase Messaging and I am receiving notification when my app is in Foreground using the Flutter Local Notifications Plugin. But I cannot receive any notification when my app is in Background
Following is my code:
// FIREBASE CONFIGURATION & HANDLING
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
debugPrint("onMessage: $message");
final notification = message['data']['notification'];
Map responseBody = convert.jsonDecode(notification);
_showNotification(
icon: responseBody['icon'],
title: responseBody['title'],
body: responseBody['body'],
);
setState(() {});
},
);
// DISPLAY NOTIFICATION - HEADS UP
Future<void> _showNotification({String icon, String title, String body}) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.Max,
priority: Priority.High,
ticker: 'ticker',
);
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
title,
body,
platformChannelSpecifics,
);
}
Can someone please guide me how can I receive notifications when my app is in Background
Resolved!!
The issue was with the format of curl calling. As per the instructions given here
Changed this:
{
"data": {
"notification": {
"title": "Notification Title",
"body": "Notification Body",
},
"click_action": "FLUTTER_NOTIFICATION_CLICK"
},
"to": "YOUR_DEVICE_TOKEN"
}
To this:
{
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
},
"notification": {
"body": "Notification Body",
"title": "Notification Title"
},
"priority": "high",
"to": "YOUR_DEVICE_TOKEN"
}

Flutter: Foreground notification is not working in firebase messaging

I need to show firebase notifications when the app is on foreground by using local notification but it is not working.
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin=new FlutterLocalNotificationsPlugin();
static FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
static StreamController<Map<String, dynamic>> _onMessageStreamController =
StreamController.broadcast();
static StreamController<Map<String, dynamic>> _streamController =
StreamController.broadcast();
static final Stream<Map<String, dynamic>> onFcmMessage =
_streamController.stream;
#override
void initState() {
super.initState();
var android=AndroidInitializationSettings('mipmap/ic_launcher.png');
var ios=IOSInitializationSettings();
var platform=new InitializationSettings(android,ios);
flutterLocalNotificationsPlugin.initialize(platform);
firebaseCloudMessaging_Listeners();
}
Here is the Firebase Code
void firebaseCloudMessaging_Listeners() {
if (Platform.isIOS) iOS_Permission();
_firebaseMessaging.getToken().then((token) {
print("FCM TOKEN--" + token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
showNotification(message);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
},
);
}
This is showNotification method
void showNotification(Map<String, dynamic> msg) async{
print(msg);
var android = new AndroidNotificationDetails(
'my_package', 'my_organization', 'notification_channel', importance: Importance.Max, priority: Priority.High);
var iOS = new IOSNotificationDetails();
var platform=new NotificationDetails(android, iOS);
await flutterLocalNotificationsPlugin.show(
0,'My title', 'This is my custom Notification', platform,);
}
and Firebase Response
{notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}}
You can find the answer in FlutterFire documentation
https://firebase.flutter.dev/docs/migration/#messaging
You just add to your code the following line
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
There is an active issue logged on GitHub repository for the package regarding the same. Firebase messaging and local notifications won't work together on iOS since you can register only a single delegate for notifications.
Check out: https://github.com/MaikuB/flutter_local_notifications/issues/111
There's also an active flutter issue for the same:
https://github.com/flutter/flutter/issues/22099
Problem:
See the Push Notification while application is in foreground.
Solution:
I was using firebase_message plugin and I was able to see the Push Notification while application is in foreground by making these few changes in my flutter project's iOS AppDelegate.swift file in flutter project.
import UIKit
import Flutter
import UserNotifications
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
// set the delegate in didFinishLaunchingWithOptions
UNUserNotificationCenter.current().delegate = self
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
}
Note:
This also works while using with flutter_local_notification plugin but with an issue that onSelectNotification is not working due to changes done above.
For not receiving the notification in foreground make sure the android drawable file contains the launcher_icon or the icon which you have set in shownotification function.
I also couldn't get firebase notifications with flutter_local_notifications working on iOS in foreground. Background notifications worked fine. Problem was that firebase notification data is different on iOS. Notification data is not ["notification"]["title"] as in android but ["aps"]["alert"]["title"].

Flutter android do not get notification when app is killed

I am using firebase console to send the notification to my android device, it's working fine when the app is in background or foreground state. But it's not receiving the notification in killed state since m not attaching any data it should not be a data notification.
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
MessageDialog messageDialog = new MessageDialog();
messageDialog.information(context, "Notification");
print('on message $message');
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
},
);
For receiving notification when an app is in background you can use onBackgroundMessage
firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
handleNotification(message);
return;
},
onBackgroundMessage: fcmBackgroundMessageHandler,
onResume: (Map<String, dynamic> message) {
print('$fcmTag on resume $message');
return;
},
onLaunch: (Map<String, dynamic> message) {
print('$fcmTag on launch $message');
return;
},
);
Here is the fcmBackgroundMessageHandler
static Future<dynamic> fcmBackgroundMessageHandler(Map<String, dynamic> message) {
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
//handleNotification(message);
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
} }
Note: I am still finding a solution to receive a solution when the app is in Kill state. I will update answer once get the solution

Categories

Resources