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.