4

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.

Phoenix
  • 467
  • 4
  • 18
  • At this [discussion](https://forums.xamarin.com/discussion/87390/fcm-notifications-custom-data-as-android-intent-extras) I got some help to solve this. If you have a Splash screen, your Intent.Extras probaly will apear there, so you can pass to you MainActivity. – Leonardo Costa Nov 06 '19 at 18:51

3 Answers3

2

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.

AndreaGobs
  • 338
  • 2
  • 18
0

Please go through this. Implement FirebaseMessagingService - Foreground notifications

You have to create FirebaseMessagingService in which you can receive RemoteMessage when your app is in foreground.

Karan Rami
  • 321
  • 2
  • 11
  • Hi, I am following that article I have been following. Your answer doesn't quite answer what I'm looking for. I am looking to process Intent extras. I have implemented a firebaseMessagingService already, the push notifications are being received. – Phoenix Apr 26 '19 at 18:13
  • When your app is in foreground you intent extra will be null and data will be received in remotemessege variable. – Karan Rami Apr 26 '19 at 18:33
  • I have my doubt about that, why else would there me a method to override called OnNewIntent. – Phoenix Apr 26 '19 at 18:51
  • Not sure about that. But once check remotemessege variable if you are getting your data there or not? – Karan Rami Apr 26 '19 at 20:51
0

Actually, as per the documentation, the correct way of handling Foreground notifications is to implement a FirebaseMessagingService

A better explanation is present here

You need to create a service class something like this:

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
       // private string TAG = "MyFirebaseMsgService";
          public override void OnMessageReceived(RemoteMessage message)
       {
           base.OnMessageReceived(message);
           string messageFrom = message.From;
           string getMessageBody = message.GetNotification().Body;
           SendNotification(message.GetNotification().Body);
       }
    void SendNotification(string messageBody)
    {
        try
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                .SetContentTitle("Title")
                .SetContentText(messageBody)
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent);

            NotificationManagerCompat notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(0, notificationBuilder.Build());
        }
        catch (Exception ex)
        {

        }
       }
     }

For more information, you can check my SO question

How to handle Firebase Notification i.e. Notification Messages and Data Messages in Android

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • Hi there, thank you for the informative answer but this doesn't answer my question. I have the implementation of the foreground, it's working, but my question is how to implement the Intent.Extras correctly. – Phoenix Apr 26 '19 at 18:12
  • What do you mean when you say `implement the Intent.Extras correctly` – FreakyAli Apr 26 '19 at 18:26
  • 1
    You are showing me code to send the push notification on the foreground. That's not what I'm asking. That part is functional. I'm looking to handle the notification when it's clicked. When clicked, I need to know what the intents are. When the push is clicked, OnNewIntent method is triggered. – Phoenix Apr 26 '19 at 18:39
  • You know it will be the same intent you are creating the pending intent with right? – FreakyAli Apr 26 '19 at 20:28
  • Hi, I'm okay with it being the same intent. One passes the extras(background), not the foreground unfortunately. My original problem. My goal is to redirect to a new page on push click event – Phoenix Apr 29 '19 at 13:26
  • A Foreground notification does not have the same intent as a background if you have a use case where you need to open different pages then why not check the message in RemoteMessage class – FreakyAli Apr 29 '19 at 13:32
  • Could you briefly show me a RemoteMessage class example? how do I trigger the page redirects with RemoteMessages? – Phoenix Apr 29 '19 at 13:52
  • Like if `RemoteMessage` has a string `Message` so using that message you must be getting `OnMessageReceived` using this string you can decide what intent goes in the pending intent once you do that you can navigate to different pages – FreakyAli Apr 29 '19 at 14:20