Xamarin Forms: How to implement notification tapping in android platform?

Sreejith Sree 1,251 Reputation points
2022-03-21T14:25:15.377+00:00

I have implemented the push notification using FCM in my xamarin forms android project. When I tap on the notification I will show the corresponding content page also.

My code for showing the notification on UI using notificationBuilder

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

    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        var notificationBuilder = new NotificationCompat.Builder(this)
                    .SetContentTitle(Header)
                    .SetSmallIcon(Resource.Drawable.icon)
                    .SetContentText(body)
                    .SetAutoCancel(true)
                    .SetContentIntent(pendingIntent);

        var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        notificationManager.Notify(0, notificationBuilder.Build());
        //notificationManager.Notify(new Random().Next(), notificationBuilder.Build());
    }
    else
    {
        var notificationBuilder = new NotificationCompat.Builder(this, Utils.CHANNEL_ID)
                    .SetContentTitle(Header)
                    .SetSmallIcon(Resource.Drawable.icon)
                    .SetContentText(body)
                    .SetAutoCancel(true)
                    .SetContentIntent(pendingIntent);


        var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

        NotificationChannel channel = new NotificationChannel(Utils.CHANNEL_ID, "FCM Notifications", NotificationImportance.Default);
        notificationManager.CreateNotificationChannel(channel);

        notificationManager.Notify(0, notificationBuilder.Build());
        //notificationManager.Notify(new Random().Next(), notificationBuilder.Build());
    }
}

I have implemented the notification tapping like below:

MainActivity:

//Background or killed mode
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);
        if (key == "webContentList") // Make it Dynamic instead of hardcoding here 
        {
            if (value?.Length > 0)
            {
                isNotification = true;
                LoadApplication(new App(value));
            }
        }
    }
}
//Foreground mode
if (FirebaseNotificationService.webContentList.ToString() != "")
{
    isNotification = true;
    LoadApplication(new App(FirebaseNotificationService.webContentList.ToString()));
    FirebaseNotificationService.webContentList = "";
}

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

On App.xaml.cs:

public App(string domain, string isNotification)
{
    //Notification tapping section
    if ((!String.IsNullOrEmpty(isNotification) && isNotification?.Length > 0) || _webContentList != null)
    {
        _webContentList = JsonConvert.DeserializeObject<List<webContentList>>(isNotification);
        MainPage = new DashBoardPage(_webContentList[0],null, isGroup);//loading the corresponsing page
    }
    else if (domain == "")//normal loading
    {
        MainPage = new SmartWCM.MainPage(domain, false);
    }
}

Currently, only one notification will show on the phone. The new notifications will clear the old one and the last notification will show on the UI. This is happening because I have set 0 as the value inside notificationManager.Notify.

notificationManager.Notify(0, notificationBuilder.Build());

If I change it like a random number like below all notification will show on UI.

notificationManager.Notify(new Random().Next(), notificationBuilder.Build());

My problem at this stage(when multiple notifications are visible on UI) how I can implement the notifications tapping? If I click any notification only the last notifications corresponding page will open. Any solution for this.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,298 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,311 Reputation points Microsoft Vendor
    2022-03-22T03:09:04.637+00:00

    Hello,​

    When you create the pendingIntent, you set requestCode to 0, please change the requestCode from 0 to new Random().Next() like following code

       var pendingIntent = PendingIntent.GetActivity(context, new Random().Next(), intent, PendingIntentFlags.OneShot);  
    

    I check the android source code of the PendingIntent, IIententSender use requestCode to distinguish different PendingIntents

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.