'Display Push Notifications on Xamarin Android when in Foreground
My understanding of "data" notifications and "Notification" notifications is that they are controlled by whether or not you are including the "notification" object in your JSON payload (I am sending these notifications via Azure Notification Hub). Ones without the "notification" object could be considered silent.
This is the JSON payload for the notification
json = new JObject
{
["notification"] = new JObject
{
["title"] = notificationRequest.Title,
["body"] = notificationRequest.Text,
},
["priority"] = "normal",
};
// Action is optional when the notification is not Silent
if (notificationRequest.Action != null)
{
json.Add(
"data",
new JObject
{
["action"] = JObject.FromObject(notificationRequest.Action),
});
}
If the app is in the background and the notification is not a silent "data only" one then the Notification goes straight into the System tray. If the app is in the foreground the OS does not send it to the system tray instead it calls the app
What I really want is for a Notification Alert to popup when the app is in the foreground or the background as it does in iOS.
A couple of threads I have read say that I should be sending data only Push Notifications and then in the OnReceived callback creating local notifications. I am using the Shiny.Notifications library to create local notifications but again they go straight to the System Tray. I have tried with and without a Channel
In my Shiny PushDelegate.OnReceived
var shinyNotificationManager = Shiny.ShinyHost.Resolve<Shiny.Notifications.INotificationManager>();
await shinyNotificationManager.AddChannel(
new Shiny.Notifications.Channel()
{
Importance = Shiny.Notifications.ChannelImportance.High,
Description = "Breathe General",
Identifier = "9999",
});
await shinyNotificationManager.Send(
new Shiny.Notifications.Notification()
{
Channel = "9999",
Message = notification.Notification.Message,
Title = notification.Notification.Title,
Android = new Shiny.Notifications.AndroidOptions() { LaunchActivityFlags = Shiny.Notifications.AndroidActivityFlags.SingleTop },
});
This SO question suggested that I need to create a Channel. Have I misunderstood how Channels should be used
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
