Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to handle NotificationCompat.Builder setTimeoutAfter

Is there a way to handle (maybe a method override) when a push notification has timed out after setting the setTimeoutAfter? I'm wanting to execute an API after the timeout period has passed.

Here is what my SendLocalNotification method looks like in my FirebaseService class. This is where I'm setting my notification timeout:

void SendLocalNotification(string body)
        {
            //Unique request code to avoid PendingIntent collision.
            var requestCode = new Random().Next();

            // accept intent
            var acceptIntent = new Intent(this, typeof(MainActivity));
            acceptIntent.SetAction("ACCEPT_ACTION");
            var pendingIntentAccept = PendingIntent.GetActivity(this, requestCode, acceptIntent, PendingIntentFlags.OneShot);

            // decline intent
            var declineIntent = new Intent(this, typeof(MainActivity));
            declineIntent.SetAction("DECLINE_ACTION");
            var pendingIntentDecline = PendingIntent.GetActivity(this, requestCode, declineIntent, PendingIntentFlags.OneShot);

            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.SingleTop);

            var notificationBuilder = new NotificationCompat.Builder(this)
                .AddAction(0, "Accept", pendingIntentAccept)
                .AddAction(0, "Decline", pendingIntentDecline)

                .SetContentTitle("Content Title")
                .SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
                .SetContentText("content text")
                .SetContentInfo("content info") // i think content info and sub-text overwrite each other
                .SetSubText("sub text")
                .SetTimeoutAfter(60000)
                .SetAutoCancel(true)
                .SetShowWhen(true)
                .SetContentIntent(pendingIntentAccept)
                .SetContentIntent(pendingIntentDecline);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
            }

            var notificationManager = NotificationManager.FromContext(this);
            notificationManager.Notify(0, notificationBuilder.Build());
        }

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Since you have set the special time(60000), you can create an alarm to execute the API.

    Besides, you can also use the following code to achieve this:

            new Handler().PostDelayed(delegate
                {
                    // Your code here
                    Toast.MakeText(this,"Test...",ToastLength.Long).Show();
                }, 60000);
    
  • MountainDewMountainDew Member ✭✭

    Thanks for the reply Jezh. Where exactly would the code in your response live? I'm not very familiar with handlers.

  • jezhjezh Member, Xamarin Team Xamurai

    Once you send the notification,you can call these codes to delay those times(60000), then to execute the action you want.

                 var notificationBuilder = new NotificationCompat.Builder(this)
                    .AddAction(0, "Accept", pendingIntentAccept)
                    .AddAction(0, "Decline", pendingIntentDecline)
    
                    .SetContentTitle("Content Title")
                    .SetSmallIcon(Resource.Drawable.laundry_basket_icon_15875)
                    .SetContentText("content text")
                    .SetContentInfo("content info") // i think content info and sub-text overwrite each other
                    .SetSubText("sub text")
                    .SetTimeoutAfter(60000)
                    .SetAutoCancel(true)
                    .SetShowWhen(true)
                    .SetContentIntent(pendingIntentAccept)
                    .SetContentIntent(pendingIntentDecline);
    
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    notificationBuilder.SetChannelId(AppConstants.NotificationChannelName);
                }
    
                var notificationManager = NotificationManager.FromContext(this);
                notificationManager.Notify(0, notificationBuilder.Build());
    
         // call these code here
    
          new Handler().PostDelayed(delegate
                {
                    // Your code here
                    Toast.MakeText(this,"Test...",ToastLength.Long).Show();
                }, 5000);
    

    Note: Maybe this has some latency, but it's negligible because the code executes very quickly.

  • MountainDewMountainDew Member ✭✭

    @jezh
    Thanks for the response. I really appreciate it. After seeing the code and thinking about it I don't think that will actually do what I need it to do. I was really hoping to tap into the notification timeout so I could send an API if it does, indeed, timeout but if the user taps the notification before the timeout then it does what it should normally do.

  • jezhjezh Member, Xamarin Team Xamurai

    Why not add the code of executing an API in the two places(1.when user tap the notification 2.wait setTimeoutAfter )?

    You can add a global variable of type bool with an initial value of false , you can put it in App.xaml.cs .

      bool hasexecuted = false;
    

    When you try to execute the API ,you can judge it:

       if (!hasexecuted) {
          //execute your API
    
         }
    
Sign In or Register to comment.