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?