locked
How to handle NotificationCompat.Builder setTimeoutAfter RRS feed

  • Question

  • User392382 posted

    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());
        }
    

    ```

    Tuesday, April 21, 2020 1:04 PM

All replies

  • User371688 posted

    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);
    
    Wednesday, April 22, 2020 6:41 AM
  • User392382 posted

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

    Wednesday, April 22, 2020 12:30 PM
  • User371688 posted

    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.

    Thursday, April 23, 2020 9:40 AM
  • User392382 posted

    @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.

    Thursday, April 23, 2020 6:22 PM
  • User371688 posted

    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
    
         }
    
    Tuesday, April 28, 2020 8:05 AM