Forum Xamarin.Forms

Notification click in Android: activity already open

ZenDroidZenDroid USMember ✭✭✭

I used Xamarin Form. I have an application with notifications that open a certain activity if I click them. I want that, if I click the notification and the activity is already opened, it's not started again, but just brought to front.
It is my code for notification:

         public void Show(string title, string body, int id, DateTime notifyTime)
        var intent = CreateIntent(id);

        var localNotification = new LocalNotification();
        localNotification.Title = title;
        localNotification.Body = body;
        localNotification.Id = id;
        localNotification.NotifyTime = notifyTime;
        if (NotificationIconId != 0)
            localNotification.IconId = NotificationIconId;
            localNotification.IconId = Resource.Drawable.Icon24;

        var serializedNotification = SerializeNotification(localNotification);
        intent.PutExtra(ScheduledAlarmHandler.LocalNotificationKey, serializedNotification);

        var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);
        var triggerTime = NotifyTimeInMilliseconds(localNotification.NotifyTime);
        var alarmManager = GetAlarmManager();

        alarmManager.Set(AlarmType.RtcWakeup, triggerTime, pendingIntent);

It is code in ScheduledAlarmHandler:

  public override void OnReceive(Context context, Intent intent)
        var extra = intent.GetStringExtra(LocalNotificationKey);
        var notification = DeserializeNotification(extra);

        var builder = new NotificationCompat.Builder(Application.Context)

        var resultIntent = LocalNotificationsService.GetLauncherActivity();
        resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
        var stackBuilder = Android.Support.V4.App.TaskStackBuilder.Create(Application.Context);
        var resultPendingIntent =
            stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

        var notificationManager = NotificationManagerCompat.From(Application.Context);
        notificationManager.Notify(notification.Id, builder.Build());

This is atribute for my MainActivity

           [Activity (Icon = "@drawable/icon", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, LaunchMode.SingleTop)]

Please help me to solve this problem.


  • I am facing the same issue, anyone?

  • PhilippSumiPhilippSumi USMember ✭✭✭

    That line here looks suspicious:

    var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);

    What happens if you try PendingIntentFlags.UpdateCurrent instead?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    The LaunchMode should not be SingleTop if you are looking to have it create a new Activity every time.

    Personally, as this is a Xamarin Forms forum, I would assume that you do want SingleTop, and the notification would use the current running activity, and then you would pass the notification clicked event on up to the Forms app for use somehow.

Sign In or Register to comment.