Reputation: 11
I have a problem when pushing a notification for an iOS device but the app is in foreground where it always pushes the notification unlike android. I wanted a similar behavior like android where it only pushes notification if in background or terminated states.
I tried utilizing setForegroundNotificationPresentationOptions
method but didn't work:
await FirebaseMessaging.instance
.setForegroundNotificationPresentationOptions(
alert: false,
badge: false,
sound: true,
);
I noticed something in its documentation:
By default, on Apple devices notification messages are only shown when the application is in the background or terminated. Calling this method updates these options to allow customizing notification presentation behavior whilst the application is in the foreground.
Important: The requested permissions and those set by the user take priority over these settings.
so it is supposed to not show alerts unless I already requested it. This showed me that the problem maybe because when requesting the permissions for firebase messaging:
static Future<bool> askForNotificationPermission() async {
final settings = await FirebaseMessaging.instance.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
return true;
}
return false;
}
I can't set the alert to false because it will prevent both foreground and background to push any alerts. How can I set the parameters only for foreground and override those values set when asking for the permission? I also searched and found that the problem maybe because of the package I am using awesome_notifications that can have some interference with default behavior. can someone guide me?
EDIT: I was not even asking the permission using firebase, I was using awesome notifications like this:
static Future<void> initializeNotificationsService() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelGroupKey: "high_importance",
channelKey: "high_importance",
channelName: 'Notifications',
channelDescription:
'Notification channel for alerting users about their scedules, chat messages and so on',
defaultColor: AppColors.primaryColor,
importance: NotificationImportance.Max,
playSound: true,
channelShowBadge: true,
),
],
channelGroups: [
NotificationChannelGroup(
channelGroupKey: "schedule_group_key",
channelGroupName: 'Schedule'),
NotificationChannelGroup(
channelGroupKey: "chat_group_key",
channelGroupName: 'Conversations'),
],
);
await AwesomeNotifications()
.isNotificationAllowed()
.then((isAllowed) async {
if (!isAllowed) {
await AwesomeNotifications().requestPermissionToSendNotifications();
}
});
await AwesomeNotifications().setListeners(
onActionReceivedMethod: onActionReceivedMethod,
onDismissActionReceivedMethod: onDismissActionReceivedMethod,
onNotificationCreatedMethod: onNotificationCreatedMethod,
onNotificationDisplayedMethod: onNotificationDisplayedMethod,
);
}
Upvotes: 1
Views: 94