I have integrated firebase-messaging plugin for notification but on clicking notification app is not opening if it is in the background or killed.I want to open app when i click on the natification.
Below is my code for same
void firebaseCloudMessagingListeners() {
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
_firebaseMessaging.getToken().then((token){
print(token);
});
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
_showNotificationWithDefaultSound(message['notification']['body']);
print('on message $message');
return;
},
onResume: (Map<String, dynamic> message) async {
_showNotificationWithDefaultSound(message['data']['body']);
print('on message $message');
return;
},
onLaunch: (Map<String, dynamic> message) async {
_showNotificationWithDefaultSound(message['data']['body']);
print('on message $message');
},
);
}
_showNotificationWithDefaultSound(message) async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
var ios = new IOSInitializationSettings();
var initSettings = new InitializationSettings(android, ios);
flutterLocalNotificationsPlugin.initialize(initSettings,
onSelectNotification: onSelectNotification);
var android1 = new AndroidNotificationDetails(
'channel id', 'channel NAME', 'channel DESCRIPTION',
importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
var ios1 = new IOSNotificationDetails();
var platform = new NotificationDetails(android1, ios1);
await flutterLocalNotificationsPlugin
.show(0, message, 'App Notification!', platform, payload: message);
}
You need add in AndroidManifest.xml
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
then, you need send click_action:FLUTTER_NOTIFICATION_CLICK in your data body of firebase notification like bellow:
DATA='{
"notification": {
"body": "this is a body",
"title": "this is a title"
},
"data": {
"att1": "value..",
"att2": "value..",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
},
"to": "<FCM TOKEN>"
}'
When your app is killed, if you click in notification, the method onLaunch is invoked, you must get message['data'] to get params.
see more here: https://stackoverflow.com/a/48405551/7105694
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With