添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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());
        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.

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.

If I change the notificationManager code to a random number like below all notifications will show on UI.

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

But at this time (when multiple notifications are visible on UI) how I can implement the notifications tapping?

You need to set new Random().Next() for var pendingIntent = PendingIntent.GetActivity(context, new Random().Next(), intent, PendingIntentFlags.OneShot); and notificationManager.Notify(new Random().Next(), notificationBuilder.Build()); at the same time. I test it in my emulator to push two different notification, it worked for different tapping.

I am using notifications for the messages which I send to groups in my application. So when I click on a notification, it must redirect the user to the corresponding group messages page.

Now I tried the latest changes on the app side and multiple notifications are viewing on the app side. But when I click on any notification only the last received notification's group page is open for all notifications. This is happening because of the below codes in OnCreate.

//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));

Every time when tapping on a notification it takes the last notification details, I have hardcoded them like that. I need to open the corresponding group message page when tapping a notification. For achieving that what change I should do to the above codes, could you please help me?

Can you share details about achieving that Background mode, and Foreground mode? You can try to push two different notifications with different pendingIntents, then set different keys in Intent.

In your MainAcitivity, based on the different key values to open different pages in forms