0

Using Xamarin.Android.

I have a BroadcastReceiver:

public class MyBroadcastReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        try
        {
            string someExtra = intent.Extras.GetString("someExtra", string.Empty);
        }
        catch (Exception)
        {
        }
    }
}

I use a Google Cloud Messaging Service and create a notification when receive some message. Using a notification builder's SetContentIntent I export some extras to the activity that will operate this notification if user opens it. And it works. But when I initialize my notification builder with SetDeleteIntent inside the GCM Service, the notification does not trigger OnReceive in the BroadcastReceiver if user swipe to dismiss the notification.

When I do all the same inside my activity (not inside the GCM service) OnReceive is triggered on every notification's swipe-to-dismiss event. But it does not work inside the service.

I have even implemented the same CreateNotification method in my activity as in the service. When I call it (to create the notification) from my activity - all works perfect. When I call it from the service using a static pointer to the current instance of the activity - MyActivity.CurrentInstance.CreateNotification(...) - my notification does not react on swipe-to-dismiss event. I even tried to put the CreateNotification's code in RunOnUiThread - no result.

Therefore, notification's swipe-to-dismiss event is triggered only in case if that notification has been created in my activity. But I create notifications when receive messages from GCM. It actually creates notifications even when application is not running.

public void CreateNotification(........) {
    //RunOnUiThread(() => {
    var notificationBuilder = new Notification.Builder(this);
    notificationBuilder.SetContentTitle(title);
    notificationBuilder.SetContentText(description);
    notificationBuilder.SetSmallIcon(Resource.Drawable.Icon);

    var notificationIntent = new Intent(this, typeof(MainActivity));
         notificationIntent.PutExtra(.......)
    var contentIntent = PendingIntent.GetActivity(Application.Context, 0, notificationIntent, PendingIntentFlags.CancelCurrent);
    notificationBuilder.SetContentIntent(contentIntent);

    // Actually my headache
    const String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
    var receiver = new MyBroadcastReceiver();
    RegisterReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
    Intent intent = new Intent(NOTIFICATION_DELETED_ACTION);
         intent.PutExtra(.......)
    var deleteIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);
    notificationBuilder.SetDeleteIntent(deleteIntent);

    var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
    notificationManager.Notify(1, notificationBuilder.Build());
    //});
}

Any ideas how to create a notification (inside the service) that will be able to trigger its swipe-to-dismiss event?

1 Answers1

0

notification's swipe-to-dismiss event is triggered only in case if that notification has been created in my activity

When you When you use SetDeleteIntent(deleteIntent) method, make sure your deleteIntent is initialized by using PendingIntent.GetBroadcast method.

GetBroadcastet means retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast(). When you use GetActivity method, it means retrieve a PendingIntent that will start a new activity. That's reason that your notification does not trigger OnReceive in the BroadcastReceiver. For more information you could read this document.

Modify your code :

 var deleteIntent = PendingIntent.GetActivity(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);

To :

 var deleteIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent);

Then in your GCM Service, your notification's swipe-to-dismiss event will be triggered .

York Shen
  • 9,014
  • 1
  • 16
  • 40
  • Thank you. GetBroadcast is a great spell. My headache has gone, thanks to your magic))) But it works only if I call CreateNotification using a static pointer to the current instance of the activity - MyActivity.CurrentInstance.CreateNotification(...). CreateNotification declared in the service does not do the magic. – Vitaliy Hanych Aug 08 '17 at 09:05
  • @Vitaliy Hanych, happy coding. : ) – York Shen Aug 08 '17 at 09:06
  • But is there any way to force BroadcastReceiver to react on the event of the notification that I create in my service? When the application is not running, I can't call MyActivity.CurrentInstance.CreateNotification(). Therefore I can operate notification's swipe-to-dismiss event only if the application is running. – Vitaliy Hanych Aug 08 '17 at 09:34
  • @Vitaliy Hanych, it's a little complex, you could post a new question for a better answer. : ) – York Shen Aug 08 '17 at 09:45
  • OK. Thank you for advice. – Vitaliy Hanych Aug 08 '17 at 10:51