I use the flutter_local_notifications
package for local notifications, but when I create a local notification I have no errors, but the notification does not arrive on iOS even though I have enabled permission to receive notifications. What could be the reason for not receiving notifications?
local notification
static final FlutterLocalNotificationsPlugin _localNotificationPlugin =
FlutterLocalNotificationsPlugin();
Future<void> initialize() async {
const AndroidInitializationSettings androidInit =
AndroidInitializationSettings('notify_icon');
DarwinInitializationSettings iosInit = DarwinInitializationSettings(
requestSoundPermission: false,
requestBadgePermission: false,
requestAlertPermission: false,
onDidReceiveLocalNotification: (id, title, body, payload) => null,
);
InitializationSettings initSettings = InitializationSettings(
android: androidInit,
iOS: iosInit,
);
await _localNotificationPlugin.initialize(initSettings);
}
// to show periodic notification at regular interval
Future showPeriodicNotifications({
required String title,
required String body,
required String payload,
required int timestamp,
}) async {
final NotificationDetails details = await _notificationDetails();
await _localNotificationPlugin.periodicallyShow(
1,
title,
body,
RepeatInterval.everyMinute,
details,
payload: payload,
);
}
// to schedule a local notification
Future showScheduleNotification({
required String title,
required String body,
required String payload,
required int timestamp,
}) async {
final NotificationDetails details = await _notificationDetails();
DateTime now = DateTime.now();
tz.initializeTimeZones();
await _localNotificationPlugin.zonedSchedule(
0,
title,
body,
// tz.TZDateTime.now(tz.local).add(const Duration(seconds: 10)),
tz.TZDateTime(
tz.local,
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second,
).add(const Duration(seconds: 5)),
details,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
payload: payload,
);
print(
tz.TZDateTime(
tz.local,
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second,
).add(const Duration(seconds: 10)),
);
}
Future<NotificationDetails> _notificationDetails() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
'channel_id',
'channel_name',
channelDescription: 'description',
importance: Importance.max,
priority: Priority.max,
);
const DarwinNotificationDetails iosNotificationDetails =
DarwinNotificationDetails(
presentAlert: true,
presentBadge: true,
presentSound: true,
);
return const NotificationDetails(
android: androidNotificationDetails,
iOS: iosNotificationDetails,
);
}
2
Answers
For iOS, you have to generate the certificate from your MAC, and then you have to upload this to your Apple developer account under your app permission while enabling the Push Notification feature. Also, you have to enable background notifications while generating the TestFlight or IPA file as well.
You can see this and this articles which show the steps in detail. I hope it helps!
add
matchDateTimeComponents
parameter in_localNotificationPlugin.zonedSchedule
method