At the moment I'm using this tutorial for xamarin push notifications. It's working like a charm. My goal now is to handle the intents properly.
In my firebase messaging service, I create a notification and Put extras.
private void CreateNotification(Object e)
{
try
{
string title = "";
string body = "";
var intent = new Intent(this, typeof(MainActivity));
var i = e as Intent;
var bundle = i.Extras;
title = bundle.GetString("gcm.notification.title");
body = bundle.GetString("gcm.notification.body");
intent.PutExtra("view", "test");
intent.PutExtra("title", title);
intent.PutExtra("body", body);
intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.CancelCurrent | PendingIntentFlags.UpdateCurrent);
Notification.Builder builder = new Notification.Builder(this);
builder.SetSmallIcon(Resource.Drawable.icon_notification);
builder.SetContentIntent(pendingIntent);
builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.icon_notification));
builder.SetContentTitle("Test");
builder.SetContentText(body);
builder.SetDefaults(NotificationDefaults.Sound);
builder.SetAutoCancel(true);
NotificationManager notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(1, builder.Build());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
At the moment background push notifications are handled perfectly. I am able to run a simple line in the OnCreate method within MainActivity.cs
Intent.GetStringExtra("view");
this gets me the value that was set in CreateNotification.
The issue I come across is trying to get the intents on the foreground. This code resides on MainActivity.cs and is triggered when a push notification is clicked on the foreground.
protected async override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
var value = Intent.Extras.GetString(key);
Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
}
}
}
Intent.Extras is always null, may I ask where abouts am I going wrong.
Maybe it's late for the answer, but it can help others.
The only error is that you are using Intent in place of intent (the passed argument)
if (Intent.Extras != null)
instead of
if (intent.Extras != null)
I fell into the same distraction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With