Forum Xamarin Xamarin.Android

Custom notification does not open activity on second click in Xamarin Android

Intent intent = new Intent(this, typeof(DrawRect)).AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);

PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

RemoteViews remoteView = new RemoteViews(PackageName, Resource.Layout.NotificationLayout);

int notificationId = 233;
            Intent buttonIntent = new Intent("button_clicked");
            buttonIntent.PutExtra("id", notificationId);
            PendingIntent buttonPendingIntent = PendingIntent.GetBroadcast(this, notificationId, buttonIntent, 0);
            remoteView.SetOnClickPendingIntent(Resource.Id.cloceNotification, buttonPendingIntent);

var notify = new NotificationCompat.Builder(this, "Local Notification")
                    .SetSmallIcon(Resource.Mipmap.icon)
                    .SetContentIntent(pendingIntent)
                    .SetDefaults(0)
                    .SetOngoing(true).SetStyle(new NotificationCompat.DecoratedCustomViewStyle())
                    .SetCustomContentView(remoteView);


                NotificationManagerCompat notificationCompat = NotificationManagerCompat.From(this);
                Notification notification = notify.Build();
                notification.Flags |= NotificationFlags.OnlyAlertOnce;
                notificationCompat.Notify(notificationId, notification);
    Finish();

Best Answer

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    I've repreduced the issue.

    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    

    PendingIntentFlags.OneShot: this PendingIntent can only be used once. If set, after Android.App.PendingIntent.Send is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

    You can change 'OneShot' to 'UpdateCurrent'

    var resultPendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
    

    PendingIntentFlags.UpdateCurrent: This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    Accepted Answer

    I've repreduced the issue.

    PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
    

    PendingIntentFlags.OneShot: this PendingIntent can only be used once. If set, after Android.App.PendingIntent.Send is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.

    You can change 'OneShot' to 'UpdateCurrent'

    var resultPendingIntent = PendingIntent.GetActivity(this, 0, resultIntent, PendingIntentFlags.UpdateCurrent);
    

    PendingIntentFlags.UpdateCurrent: This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

  • VINNUSAURUSVINNUSAURUS Member ✭✭

    Nicely explained, thank you. :)

Sign In or Register to comment.