How to make on click notification IOS works - flutter

i hope you're doing good. I'm trying to solve one problem: everything woks fine, the notifications works fine on iOS but when i try to execute a code ( showDialog ) for the notification it doesnt works. I'm using onSelectedNotification: function to execute something, in Android it works perfectly but in iOS it's just not opening. Follow the initState
FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification=message.notification;
AndroidNotification? android=message.notification?.android;
if(notification!=null && android!=null){
fltNotification.show(
notification.hashCode, notification.title, notification.
body, generalNotificationDetails);
}
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {// I've tried to use the function here too but it not works
});
Follow the configs for HomePage:
var androiInit = AndroidInitializationSettings('#mipmap/ic_launcher');//for logo
var iosInit = IOSInitializationSettings();
IOSNotificationDetails(presentAlert: true, presentBadge: true, presentSound: true);
var initSetting=InitializationSettings(android: androiInit,iOS:
iosInit);
flutterLocalNotificationsPlugin.initialize(initSetting, onSelectNotification: onSelectNotification);
And i'm using this onSelectNotification on flutterlocalnotificationsplugin to run my function ( showDialog ) but in IOS it not works. What can i do? Am I missing something?

Related

Flutter: FCM background notification large icon

I'm using firebase_messaging and flutter_local_notifications for notifications, and I was able to show a large icon in case of local(foreground) notifications, but I didn't seem to find a way to show the same large icon when the app is background.
handle background noti and show noti using local notification.
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
This code help you to receive a Notification and to use in your app :
void initMessaging() {
var androiInit = AndroidInitializationSettings(‘#mipmap/ic_launcher’);//for logo
var iosInit = IOSInitializationSettings();
var initSetting=InitializationSettings(android: androiInit,iOS:
iosInit);
fltNotification = FlutterLocalNotificationsPlugin();
fltNotification.initialize(initSetting);
var androidDetails =
AndroidNotificationDetails(‘1’, ‘channelName’, ‘channel
Description’);
var iosDetails = IOSNotificationDetails();
var generalNotificationDetails =
NotificationDetails(android: androidDetails, iOS: iosDetails);
FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification notification=message.notification;
AndroidNotification android=message.notification?.android;
if(notification!=null && android!=null){
fltNotification.show(
notification.hashCode, notification.title, notification.
body, generalNotificationDetails);
}
});
}
If you have any difficulty, please check this article : Flutter Push Notification Medium.
Thanks.
A better way to use largeIcon is using the image property in notification payload when you are sending the notification:
const payload = {
notification: {
title: 'title',
body: 'description',
image: 'large_icon_url',
sound : "default"
},
};
1- use flutter_local_notifications in backgroundMessageHandler to display the image
2- remove the "notification" in the payload and add your data in
"data:{
title: 'title',
body: 'description',
image: 'image',
}"

How to handle click on push-notification when application is in foreground?

I use the Firebase Cloud Messaging (FCM) in my Flutter project. The following packages:
firebase_messaging: ^8.0.0-dev.15
flutter_local_notifications: ^4.0.1+2
I know that I can handle click on push-notification when app is in background in the following way:
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
//TODO
});
and it works fine, but how to handle click on push-notification when application is in foreground? Can't find in the documentation as well. Could you please help me. Thanks in advance.
you can set the callback function on initializing
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
),
onSelectNotification: handleClickNotification,
);
handleClickNotification(String? payload) {
//your code
}
Use this dependency when the app is in foreground- https://pub.dev/packages/flutter_local_notifications
Edit
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
//TODO
});
You are already using this code for when the app is in background. See that you are using onMessageOpenedApp. For firing the message when in foreground you can use onMessage. Example-
FirebaseMessaging.onMessage.listen((message) {
LocalNotificationService.display(message);
});

Flutter Local Notifications doesn't execute code with app terminated

I have an app that schedules alarms, which trigger flutter local notifications. When pressing one of the buttons on these notifications, the database gets updated to reflect that you've turned off the alarm.
However, when the app is terminated/closed down, it seems as if no code gets executed and the database doesn't get updated.
The notification does get triggered, it's when the user presses a button inside of it that no code gets executed. Looks something like this
I'd like to know if what I'm trying is even possible, because at this point I'm not sure.
The notifications are created using the zonedSchedule from the flutter local notifications plugin.
The app does not get opened when pressing one of the buttons in the notification.
The code is as follows:
class LocalNoticationsService {
LocalNoticationsService(
this._notificationsPlugin,
);
final FlutterLocalNotificationsPlugin _notificationsPlugin;
Future<void> init() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings(
'img/path',// path to notification icon
);
final DarwinInitializationSettings initializationSettingsIOS = DarwinInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
defaultPresentAlert: true,
defaultPresentSound: true,
notificationCategories: [
DarwinNotificationCategory(
'category',
options: {
DarwinNotificationCategoryOption.allowAnnouncement,
},
actions: [
DarwinNotificationAction.plain(
'snoozeAction',
'snooze',
),
DarwinNotificationAction.plain(
'confirmAction',
'confirm',
options: {
DarwinNotificationActionOption.authenticationRequired,
},
),
],
),
],
);
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);
await _notificationsPlugin.initialize(
initializationSettings,
onSelectNotification: _onSelectedNotification,
onSelectNotificationAction: onSelectNotificationAction,
);
final notificationOnLaunchDetails = await _notificationsPlugin.getNotificationAppLaunchDetails();
if (notificationOnLaunchDetails?.didNotificationLaunchApp ?? false) {
_onSelectedNotification(notificationOnLaunchDetails!.payload);
}
}
void _onSelectedNotification(String? payload) {
// Not relevant to this problem
}
Future<bool> _confirmAlarm(AlarmPayLoad alarmPayload) async {
// Update database
}
}
// Top level function
Future<void> onSelectNotificationAction(NotificationActionDetails details) async {
await Firebase.initializeApp();
final localNotificationsService = LocalNoticationsService(
FlutterLocalNotificationsPlugin()
);
await localNotificationsService.init();
final alarmPayload = AlarmPayLoad.fromJson(details.payload);
await localNotificationsService._confirmAlarm(alarmPayload)
}
Due to this all running in the background, I've been unable to find a way to log things. I've tried using the http package to send post request to a webhook with some data, but these do not seem to trigger when the app is terminated, but do work when in the app is in the foreground or in the background(not closed).
Any post or videos I've found online about flutter local notifications don't seem to cover this problem or are using firebase push notifications and the associated package for background handling.
I'd be very interested to hear any feedback on what might be going wrong or what might be the solution and how handling background notifications works. If you need additional information on the code, feel free to ask.
If you want to run it on background, follow the link here.

How to add android notification channel id for flutter app to fix notification while app is on background

In my flutter app the function onResume and onLunch not working on android platform, while they work fine on IOS,
i receive the following message on console instead of printed strings in those functions:
"W/FirebaseMessaging(24847): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used."
onMessage function works fine, the problem is when the app is in background
my guess is that it has something to do with android notification channel id which should be added in android manifest
when i add that to manifest with adding the following code to AndroidManifest the message change to :
(I added a strings.xml file in values and defined "default_notification_channel_id" there.)
"Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used."
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="#string/default_notification_channel_id"/>
in my console i should receive onResume and onLunch strings which i printed , but instead i receive following messages :
"W/FirebaseMessaging(24847): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used."
"Notification Channel set in AndroidManifest.xml has not been created by the app. Default value will be used."
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="#string/default_notification_channel_id"/>
You should create a notification channel first, the plugin: flutter_local_notifications can create and remove notification channels.
First, initialize the plugin:
Future<void> initializeLocalNotifications() async {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// app_icon needs to be a added as a drawable resource to the
// Android head project
var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: selectNotification);
}
Second, create notification channel using this plugin:
Future<void> _createNotificationChannel(String id, String name,
String description) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
id,
name,
description,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);
}
Now you have two options:
Create a channel with the default id you already defined in AndroidManifest.xml
Choose your own channel ids and send each notification to a specific channel by embedding the channel id into your FCM notification message. To do so, add channel_id tag to your notification under notification tag, under android tag.
Here is a sample notification message from Firebase Functions with custom notification channel id:
'notification': {
'title': your_title,
'body': your_body,
},
'android': {
'notification': {
'channel_id': your_channel_id,
},
},
have you created default_notification_channel_id ?
"W/FirebaseMessaging(24847): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used."
You have this warning when you don't set the channel_id in the payload of the notification

Running specific function in background in an flutter application

I am developing a water remainder app in a flutter. I want a specific function to run on background even when the app is killed. I have a function that gives notification. I want that notification function to run on background even when the app is not running on the background, but I don't know where to call that function.
This is the notification function:
_scheduleNotify() async {
print("object");
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('#mipmap/ic_launcher');
var ios = new IOSInitializationSettings();
var initsetting = new InitializationSettings(android, ios);
flutterLocalNotificationsPlugin.initialize(initsetting);
var scheduledNotificationDateTime =
new DateTime.now().add(new Duration(seconds: 5));
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
I have called this function every time the user gives input of intake. This works fine until the app is running. But if the app is closed or killed. This is not working. So where should I call this function to make this run even when the app is killed?
I didn't get any resources to implement this and I am a beginner in a flutter. Can someone help me with this? Thanks in advance.
The recommendation from this SO post could be applicable to your case:
For reminders i would recommend Flutter Local Notifications
Plugin. It has
a powerful scheduling api. From the documentation of local
notification:
And for push notification, you can use Firebase Cloud
Messaging or one signal
plugin or you can
implement natively through
platform-channels
Edit: You can also fire notifications according to specific conditions
even if the app is terminated. This can be achieved by running dart
code in the background. Quoting from the official faq: