I'm wondering in what class can I specify how to handle a notification that gets sent while the app is either closed or in the background. Right now I get notifications locally which then triggers my SendLocalNotification
method to structure it the way that I'd like and this is what I'm wanting to happen for notifications while the app is closed or in the background too. I still receive background notifications but they don't trigger any of the code that structures what it should look like.
I did some digging and read a few things about overriding a method to handle remote notifications but couldn't quite find a good example or even the specific method to override.
Here's my OnMessageReceived override in my FirebaseService class (ignore the code that looks out of place. I've been messing around with stuff):
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
string messageBody = string.Empty;
if (message.GetNotification() != null)
{
switch (message.GetNotification().Title)
{
case "Load Matched":
break;
}
messageBody = message.GetNotification().Body;
}
else
{
messageBody = message.Data.Values.First();
}
try
{
MessagingCenter.Send(messageBody, "Update");
}
catch (Exception e)
{
}
SendLocalNotification(messageBody);
}
And here is my SendLocalNotification method. I'd love for remote notifications to also trigger this method so they could look the same.
void SendLocalNotification(string body)
{
//Unique request code to avoid PendingIntent collision.
var requestCode = new Random().Next();
// accept intent
var acceptIntent = new Intent(this, typeof(MainActivity));
acceptIntent.SetAction("ACCEPT_ACTION");
var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);
// decline intent
var declineIntent = new Intent(this, typeof(MainActivity));
declineIntent.SetAction("DECLINE_ACTION");
var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.SingleTop);
var notificationBuilder = new NotificationCompat.Builder(this)
.AddAction(0, "Accept", pendingIntentAccept)
.AddAction(0, "Decline", pendingIntentDecline)
.SetContentTitle("Content Title")
.SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
.SetContentText("content text")
.SetContentInfo("content info")
.SetSubText("sub text")
.SetAutoCancel(true)
.SetShowWhen(true)
.SetContentIntent(pendingIntentAccept)
.SetContentIntent(pendingIntentDecline);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
}
EDIT: Here is the code that I'm using to send my notification. I was under the impression that having a data
tag meant that a data notification was being sent which would then get picked up by OnMessageReceived
.
public void SendNotificationByTag(string tag, string notificationBody = "", string notificationTitle = "")
{
var url = $"{_baseUrl}/messages/?api-version=2015-01";
var client = new RestClient($"{url}");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("ServiceBusNotification-Format", "gcm");
request.AddHeader("ServiceBusNotification-Tags", $"{tag}");
request.AddHeader("Authorization", $"{NotificationHelper.CreateSasToken(url, "DefaultFullSharedAccessSignature", $"{_configuration["DefaultFullSharedAccessSignature"]}")}");
request.AddParameter("application/json", $"{{\"data\":\n\t{{\n\t\t\"gcm.notification.body\":\"{notificationBody}\", \n\t\t\"gcm.notification.title\":\"{notificationTitle}\",\n\t}}\n}}", ParameterType.RequestBody);
client.Execute(request);
}