The plugin works great when testing it on Android, but when I try it on iOS simulator, nothing happens. I don't use any other notification plugins like firebase messaging so there shouldn't be any conflict. Below is my code and logs.
I initialize the plugin in the Home Screen after login.
void initState() {
super.initState();
var initializationSettingsAndroid = new AndroidInitializationSettings('app_icon');
var initializationSettingsIos = new IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveNotification
);
var initializationSettings = new InitializationSettings(initializationSettingsAndroid, initializationSettingsIos);
notificationsPlugin = new FlutterLocalNotificationsPlugin();
notificationsPlugin.initialize(initializationSettings, onSelectNotification: onSelectNotification);
print(initializationSettings.ios.toMap());
}
This is what prints for ios initialization settings
flutter: {requestAlertPermission: true, requestSoundPermission: true, requestBadgePermission: true, defaultPresentAlert: true, defaultPresentSound: true, defaultPresentBadge: true}
Then I pass the plugin instance to the other pages and use it to schedule notifications for their content.
I set the notification details in the initState
var androidSpecifics = new AndroidNotificationDetails('channelId', 'channelName', 'channelDescription');
var iosSpecifics = new IOSNotificationDetails(presentAlert: true, presentSound: false, presentBadge: false);
notificationDetails = new NotificationDetails(androidSpecifics, iosSpecifics);
print(notificationDetails.iOS.toMap());
iOS Notification Details prints
flutter: {presentAlert: true, presentSound: false, presentBadge: false, sound: null}
refreshing the notifications on Firestore document changes.
void refreshNotifications() async {
QuerySnapshot snapshot = await Firestore.instance.collection('settings').where('owner', isEqualTo: widget.userId).getDocuments();
int monthsDue = 6;
List<dynamic> daysBefore = [0, 30];
if(snapshot.documents.isNotEmpty) {
Setting setting = Setting.fromMap(snapshot.documents.first.data);
if(setting.addNotifications != null && !setting.addNotifications) return;
monthsDue = setting.acftMonths ?? 6;
daysBefore = setting.acftNotifications ?? [0, 30];
}
//get pending notifications and cancel them
List<PendingNotificationRequest> pending = await widget.notificationsPlugin.pendingNotificationRequests();
pending = pending.where((pr) => pr.payload == 'ACFT').toList();
print('Pending Notification: $pending');
for(PendingNotificationRequest request in pending) {
widget.notificationsPlugin.cancel(request.id);
}
int startingId = prefs.getInt('runningId') ?? 0;
List<List<String>> dates = [];
//create copy of documents
List<DocumentSnapshot> docs = List.from(documents);
//sort by date
docs.sort((a, b) => a['date'].toString().compareTo(b['date'].toString()));
//combine Soldiers with like dates
for(int i = 0; i < docs.length; i++) {
if(i == 0) {
dates.add(['${docs[i]['rank']} ${docs[i]['name']}, ${docs[i]['firstName']}', docs[i]['date']]);
} else if(docs[i]['date'] == docs[i-1]['date']) {
dates[i-1][0] = dates[i-1][0] + ', ${docs[i]['rank']} ${docs[i]['name']}, ${docs[i]['firstName']}';
} else {
dates.add(['${docs[i]['rank']} ${docs[i]['name']}, ${docs[i]['firstName']}', docs[i]['date']]);
}
}
print(dates);
//add notifications
for(List<String> date in dates) {
if(date[1] != '') {
DateTime dueDate = DateTime.tryParse(date[1]);
dueDate = dueDate.add(new Duration(days: 30 * monthsDue, hours: 6));
if(dueDate.isAfter(DateTime.now())) {
for (int days in daysBefore) {
DateTime scheduledDate = dueDate.add(new Duration(days: -days));
if(scheduledDate.isAfter(DateTime.now())) {
print('Scheduling Notification for $scheduledDate');
widget.notificationsPlugin.schedule(
startingId,
'ACFT(s) due in $days days',
date[0],
DateTime.now().add(Duration(seconds: 10)), //schedule for 10 sec for testing purpose
notificationDetails,
payload: 'ACFT',
);
startingId++;
}
}
}
}
}
if(startingId > 1000000) startingId = 0;
prefs.setInt('runningId', startingId);
pending = await widget.notificationsPlugin.pendingNotificationRequests();
pending = pending.where((pr) => pr.title.contains('ACFT')).toList();
print('Pending Notifications: $pending');
print('Running Notification Id: $startingId');
}
And this is what is printing
flutter: Pending Notification: []
flutter: [[SSG Username, Benjamin, 2019-06-04], [SPC Two, Juan, 2019-06-05]]
flutter: Scheduling Notification for 2019-12-02 05:00:00.000
flutter: Pending Notification: []
flutter: Running Notification Id: 36
There should be one instance of PendingNotificationRequest in the second print of "Pending Notification" and no notification fires. I've also tried the .show method, but it still doesn't work for iOS. Like I said, everything works great on Android. Is there something I am missing on iOS implementation?
I'm not seeing what might be the cause here based on the code you've shown. Did you perhaps mistakenly said no to allowing your app permissions to show notifications?
You're right, I wasn't receiving a request for notification permission so I uninstalled it from my simulator and reran it. It asked for notification permission when initializing and now it works. Thank you.
Hi guys.
Can anyone suggest how to use both local_notificaions and firebase_notifications together.
I just saw this.

But I need to show a local notification when app is in foreground.
@dev-shanghai
I need the exact same thing, in the simulator if it is showing the local notification, but not on a physical device.
Most helpful comment
Hi guys.
Can anyone suggest how to use both local_notificaions and firebase_notifications together.

I just saw this.
But I need to show a local notification when app is in foreground.