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

  • Question

  • User378860 posted
    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();
    
    Tuesday, March 12, 2019 2:47 PM

Answers

  • User382871 posted

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, March 13, 2019 8:48 AM

All replies

  • User382871 posted

    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.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, March 13, 2019 8:48 AM
  • User378860 posted

    Nicely explained, thank you. :)

    Wednesday, March 13, 2019 9:24 AM