-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug] Change the title and body of incoming Push Notifications BEFORE showing the banner #95
Comments
Ok, interesting. I'm currently bit out of time but I can come back to this when the dust settles. |
Thanks for the quick reply. |
Ok, I understand. The thing with the missing documentation is, this project was created because there was nothing ready-to-use when we migrated to .NET MAUI. The CrossGeeks plugin worked nicely for us but it was full of static code and lots of things were bit overly complicated (or I was too stupid to understand it). So, bear with me if not everything is shiny in this library. So, I take your case as a new use case and try to extend the project accordingly.
|
I'd also love an option to handle building the notification by myself. In my case I get notification info on my server from 3rd party API and the information sent is very limited. I get only the ID of occured event but not any description so I can only send some generic notification. Btw. I can't get more details on server since public access token to 3rd party API is stored on user's device and private access token is stored on server for safety. By being able to build the notification I could get cached data from SQLite and show more details :) |
Not sure how helpful is this information but something similar is done in here https://github.com/TobiasBuchholz/Plugin.Firebase/blob/development/docs/cloud_messaging.md#overriding-firebasecloudmessagingimplementationnotificationbuilderprovider I've tried using that package but coulnd't get it to work. Maybe I'm too stupid but I kept getting random error after random error. |
I‘ll have a look at it. |
This can be easily fixed. Implement a custom NotificationBuilder: public class CustomNotificationBuilder : NotificationBuilder {
public CustomNotificationBuilder(ILogger<NotificationBuilder> logger, FirebasePushNotificationOptions options) : base(logger, options) {
}
public override void OnNotificationReceived(IDictionary<string, object> data) {
//manipulate data
base.OnNotificationReceived(data);
}
} In your MauiProgram.cs register the custom builder: .UseFirebasePushNotifications(o => {
#if ANDROID
builder.Services.AddSingleton<INotificationBuilder, CustomNotificationBuilder>();
#endif
} |
I guess the idea was to manipulate the notification content even before it arrives in CustomNotificationBuilder? The NotificationReceived event would still contain the original notification data. |
I was looking at the code to see if I can maybe implement something myself but sadly I barely have any free time lately so I didn't do much research but taking part of current code as example Lines 171 to 175 in 01b5e01
I'd like to be able to do something like var messageBody = this.GetNotificationBody(data);
//when you send a notification you can specify additional data in json, this would retrieve it
int eventId = GetEventId(messageBody);
//get some personal data from local database about user based on event id,
//return some custom notification text
string newText = GetSomeNewNotificationText(eventId);
if (!string.IsNullOrEmpty(messageBody))
{
notificationBuilder.SetContentText(newText);
} So it would go like: @AlleSchonWeg does your suggestion allows for this? |
Implement the service as descriped above. Then manipulate the data parameter: data["body"]="my value" |
Request for change: Method What if we place a new just before we do anything else with the notification data. We could even have a sequential pipe of processors. Some contain custom logik like those mentioned by @OvrBtn. The dev could register 0-n With this solution we could also eliminate some of the pre-processor logic inside method Let me know what you think. |
@AlleSchonWeg I did try what you proposed and it doesn't work. It seems like the CustomNotificationBuilder is not used and the overrided method is never fired. (Yes I did the @thomasgalliker I think it sounds good. |
@OvrBtn It should work. Keep in mind that you need data only payload: https://firebase.google.com/docs/cloud-messaging/concept-options?hl=de |
@AlleSchonWeg thanks for reply, sadly it doesn't work. I have #if ANDROID
builder.Services.AddSingleton<INotificationBuilder, CustomNotificationBuilder>();
#endif
builder
.UseMauiApp<App>()
...
.UseFirebasePushNotifications()
//I wasn't sure which place is better to register it so I tried before and after UseFirebasePushNotifications
#if ANDROID
builder.Services.AddSingleton<INotificationBuilder, CustomNotificationBuilder>();
#endif public class CustomNotificationBuilder : NotificationBuilder
{
public CustomNotificationBuilder(ILogger<NotificationBuilder> logger, FirebasePushNotificationOptions options) : base(logger, options)
{
}
public override void OnNotificationReceived(IDictionary<string, object> data)
{
string serialized = JsonSerializer.Serialize(data);
Log.Warn("TESTING", serialized);
Preferences.Set("test", serialized);
data["title"] = "123";
data["body"] = "123";
base.OnNotificationReceived(data);
}
} Notification I send
When starting the app I'm checking if the Preference with key "test" is set and it never is. Is there something more needed to receive the data only notification when app is in background/killed just as normal notifications? |
Hi,
One more thing. Sometimes if a debugger is attached it is not working. Try publish the app start the app and then kill the app. |
You can implement |
I need to change the title and body and sometimes the icon of the incoming Push Notification and THEN show the banner to the user to click on.
I tried it by using a CustomNotificationBuilder implementing INotificationBuilder and also by using a CustomPushNotificationHandler implementing IPushNotificationHandler but nothing worked as expected. (so maybe i should have marked this as a bug?)
Problem with the CustomNotificationBuilder:
The OnNotificationReceived() gets triggered before the banner is shown - so far so good. But when I alter the content, nothing happens and no banner is shown. When I alter the content and make with it a new notification via NotificationManagerCompat the OnNotificationReceived() is triggered again and we have an infinite loop.
Problem with the CustomPushNotificationHandler:
The OnReceived() Handler is never triggered. The banner is shown upon arrival of the push notification as if there is no handler. By the way, the OnOpened() handler works, but that doesn't help.
Please implement a solution similar to the Plugin.FirebasePushNotification by Rendy del Rosario where you could build a custom handler that did the things mentioned above and it worked.
The text was updated successfully, but these errors were encountered: