Forum Xamarin Xamarin.Android

Notification click to run Activity


I'm having an issue with trying to run an activity when a notification is clicked.

Below is my code taken straight from so it should work.

The activity I'm trying to run will open up text app with pre-defined text. At the moment I'm just trying to run new Activity code when this notification is clicked

// When the user clicks the notification, SecondActivity will start up.
Intent resultIntent = new Intent(this, typeof(SMSActivity));

            // Construct a back stack for cross-task navigation:
            TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this);
            stackBuilder.AddParentStack(Class.FromType(typeof(SMSActivity))); <--- WHERE EXCEPTION IS THROWN

            // Create the PendingIntent with the back stack:            
            PendingIntent resultPendingIntent =
                stackBuilder.GetPendingIntent(0, (int)PendingIntentFlags.UpdateCurrent);

            // Build the notification:
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .SetAutoCancel(true)                    // Dismiss from the notif. area when clicked
                .SetContentIntent(resultPendingIntent)  // Start 2nd activity when the intent is clicked.
                .SetContentTitle("Button Clicked")            // Display the count in the Content Info
                .SetSmallIcon(Resource.Drawable.Icon)  // Display this icon
                    "The button has been clicked {0} times.")); // The message to display.

            // Finally, publish the notification:
            NotificationManager notificationManager =
            notificationManager.Notify(Level5NotificationId, builder.Build());

Best Answer


  • Luis_dmLuis_dm USMember ✭✭✭

    public void notificacioninicio()
    //Notificaciones Creador
    Intent intent = new Intent(this, typeof(Inicio)); // Your activity to where you want to go

            // Create a PendingIntent; we're only using one PendingIntent (ID = 0):
            const int pendingIntentId = 0;
            PendingIntent pendingIntent = PendingIntent.GetActivity(this, pendingIntentId, intent, PendingIntentFlags.OneShot);
            // Instantiate the builder and set notification elements, including pending intent:
            Notification.Builder builder = new Notification.Builder(this)
             .SetContentTitle("Una notificacion UP:")   //titulo
             .SetContentText("DALE CLIC") //texto a mostrar
             .SetAutoCancel(true)  //cerrar notificación 
               .SetPriority(2) // prioridad de la notificación
               .SetSmallIcon(Resource.Drawable.ic_notificaciones); //icono pequeño de la notificación
            builder.SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.Icon));  //icono más grande que el anterior
            builder.SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate);  //sonido y vibración según la configuración del teléfono
            Notification notification = builder.Build();
            // Get the notification manager:
            NotificationManager notificationManager =
                GetSystemService(Context.NotificationService) as NotificationManager;
            // Mostrar la notificacion:
            const int notificationId = 0;
            notificationManager.Notify(notificationId, notification);

    I hope you have served

  • DavidMannDavidMann USMember
    edited June 2016

    Thanks for the reply,
    it's still throwing the same exception at this line;
    PendingIntent pendingIntent = PendingIntent.GetActivity(this, pendingIntentId, intent, PendingIntentFlags.OneShot);

    My Activity that i'm loading contains only this code inside the OnCreate method;

            // Create your application here
            //SMS intent to go to messaging app
            var smsUri = Android.Net.Uri.Parse("smsto:074*****831");
            var smsIntent = new Intent(Intent.ActionSendto, smsUri);
            smsIntent.PutExtra("sms_body", "****Level 5 Aler t");

    Would this cause it to crash?


  • DavidMannDavidMann USMember


    I think there is a bug when trying to use this code inside a FragmentActivity as it works fine on my other activity's but not the one with tabs.

  • What if someone wants to click on the Notification Button to make a Toast Message

Sign In or Register to comment.