Skip to content

[Enhancement] Allow iOS, like Android, to customize the push notifications #83

@GHGiampy

Description

@GHGiampy

Summary

Currently using the NotificationReceived event, only on Android, we can change the behavior of the push notification before it is processed simply modifying the Data dictionary in the FirebasePushNotificationDataEventArgs received.
This request allows also iOS to customize the push notification.

API Changes

No API changes are required, the workaround is to move a simple line of code a couple of rows earlier.
We are using the v2.5.13, so supposing it is the main branch, moving the line 273 after the 267 here:

var data = notification.Request.Content.UserInfo.GetParameters();
var notificationPresentationOptions = GetNotificationPresentationOptions(data);
this.logger.LogDebug(
$"WillPresentNotification: UNNotification.Request.Identifier \"{notification.Request.Identifier}\", " +
$"UNNotificationPresentationOptions={notificationPresentationOptions}");
this.HandleNotificationReceived(data);

allows the notificationPresentationOptions to be filled based on the Data modified by the NotificationReceived event, as a consequence this allows us to customize the push notification, exactly as in Android, before it is handled or created.

The same can be achieved for the develop branch here:

var data = notification.Request.Content.UserInfo.GetParameters();
var notificationPresentationOptions = GetNotificationPresentationOptions(data, this.options.iOS.PresentationOptions);
this.logger.LogDebug(
$"WillPresentNotification: UNNotification.Request.Identifier \"{notification.Request.Identifier}\", " +
$"UNNotificationPresentationOptions={notificationPresentationOptions}");
this.HandleNotificationReceived(data);

Moving the line 327 after the 321.

Intended Use Case

With this modification we can use code like this:

_firebasePushNotification.NotificationReceived += (s, e) =>
{
  // Event "NotificationReceived" (with app in foreground)
  _logger.LogDebug("Firebase-NotificationReceived - Data: {data}", e.Data);
#if ANDROID || IOS
  // Force the high priority when the app is in foreground to show the notification as badge
  var _isInBackground = ((App)(App.Current!)).IsInBackground;
  if (!_isInBackground && !e.Data.ContainsKey("priority")) e.Data.Add("priority", "high");
#endif
};

Which works on both, Android and iOS.

Metadata

Metadata

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions