I completed receiving test notification from FCM console. Now I am trying to open a page when tapping the notification. Any ideas about how to achieve this? I have searched the internet but can't find a working solution. I am also able to send the notification through the postman.

2 Answers

0
Sreejith Sree On Best Solutions

I handle the notification tapping in following way. The page loading is handled in App.xaml.cs.

On OnCreate():

//Background or killed mode
if (Intent.Extras != null)
{
    foreach (var key in Intent.Extras.KeySet())
    {
        var value = Intent.Extras.GetString(key);
        if (key == "webContentList") 
        {
            if (value?.Length > 0)
            {
                isNotification = true;
                LoadApplication(new App(domainname, value));
            }
        }
    }
}
//Foreground mode
if (FirebaseNotificationService.webContentList.ToString() != "")
{
    isNotification = true;
    LoadApplication(new App(domainname, FirebaseNotificationService.webContentList.ToString()));
    FirebaseNotificationService.webContentList = "";
}

//Normal loading
if (!isNotification)
{
    LoadApplication(new App(domainname, string.Empty));
}

On FirebaseNotificationService:

[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FirebaseNotificationService : FirebaseMessagingService
{
    public static string webContentList = "";
    public override void OnMessageReceived(RemoteMessage message)
    {
        base.OnMessageReceived(message);
        webContentList = message.Data["webContentList"];

        try
        {
            SendNotificatios(message.GetNotification().Body, message.GetNotification().Title);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:>>" + ex);
        }
    }

    public void SendNotificatios(string body, string Header)
    {
        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
        else
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

            var notificationBuilder = new Android.App.Notification.Builder(this, Utils.CHANNEL_ID)
                        .SetContentTitle(Header)
                        .SetSmallIcon(Resource.Drawable.icon)
                        .SetContentText(body)
                        .SetAutoCancel(true)
                        .SetContentIntent(pendingIntent)
                        .SetChannelId(Utils.CHANNEL_ID);

            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return;
            }

            var channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.High)
            {
                Description = "Firebase Cloud Messages appear in this channel"
            };

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);

            notificationManager.Notify(0, notificationBuilder.Build());
        }
    }
0
Umar3x On

I don't know what's your actual Firebase implementation, but this might help you.

There is a nice package for Firebase in Xamarin Forms that we use in our production App made by CrossGeeks team. Its working great and has all handlers for your needs. This works with iOS and Android and you don't need to write platform specific code, only configuration and some code in AppDelegate.cs and MainActivity.cs

https://github.com/CrossGeeks/FirebasePushNotificationPlugin/blob/master/docs/FirebaseNotifications.md#notification-events

I wrote a simple PushNotificationService that processes automatic refresh and/or pushes new pages considering push notif data.

When app is closed and user clicks on the notification, I store the push notif data using Akavache.

 CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>
            {
                if (App.AppBeenResumed)
                {
                    await BlobCache.UserAccount.InsertObject("pushNotifData", p.Data);
                }
                else
                {
                    await ProcessReceivedPushNotification(p.Data);
                }
            }; 

And on the landing page of the app, I check if there is an existing push notif data in the OnAppearing method of the page.

protected override void OnAppearing()
        {
            base.OnAppearing();
            App.AppBeenResumed = false;
            HandlePushNotificationIfExists();
        }

 private async void HandlePushNotificationIfExists()
        {
            IDictionary<string, object> pushNotifData;
            try
            {
                pushNotifData = await BlobCache.UserAccount.GetObject<IDictionary<string, object>>("pushNotifData");
            }
            catch (KeyNotFoundException)
            {
                pushNotifData = null;
            }

            if (pushNotifData == null) return;
            await BlobCache.UserAccount.InvalidateAllObjects<IDictionary<string, object>>();
            await PushNotificationService.ProcessReceivedPushNotification(pushNotifData);
        }

In the ProcessReceivedPushNotification you can do whatever you want ... push directly the page or whatever... call another service that will do the job of pushing a new page and some business process.

Note that App.AppBeenResumed is a static bool to determine if App has been started or resumed to handle correctly the process of treatment of the push notif (process it instant or store it in blobcache to treat it later when landing page is Appearing).

In MainActivity.cs :

 protected override void OnCreate(Bundle bundle)

        {
           ...
           LoadApplication(new App(true));
        }

In the App.cs :

 public App(bool beenResumedOrStarted)
        {
            ...
            AppBeenResumed = beenResumedOrStarted;
            ...
        }

    protected override void OnResume()
    {
        AppBeenResumed = false;
    }


    protected override void OnSleep()
    {
        //iOS states are not the same so always false when device is iOS
        AppBeenResumed = Device.RuntimePlatform != Device.iOS;
    }