BroadcastReceiver 不会对在 GmcService 中创建的通知做出反应

BroadcastReceiver does not react on notifications created in GmcService

使用 Xamarin.Android.

我有一个广播接收器:

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

我使用 Google 云消息服务并在收到消息时创建通知。使用通知生成器的 SetContentIntent,我将一些额外内容导出到 activity,如果用户打开它,它将操作此通知。它有效。但是当我在 GCM 服务中使用 SetDeleteIntent 初始化我的通知构建器时,如果用户滑动以关闭通知,通知不会触发 BroadcastReceiver 中的 OnReceive。

当我在我的 activity 中(而不是在 GCM 服务中)执行所有相同操作时,OnReceive 会在每个通知的滑动关闭事件上触发。但它在服务内部不起作用。

我什至在 activity 中实现了与服务中相同的 CreateNotification 方法。当我从 activity 调用它(创建通知)时 - 一切正常。当我使用指向 activity - MyActivity.CurrentInstance.CreateNotification(...) 当前实例的静态指针从服务调用它时 - 我的通知不会对滑动关闭事件做出反应。我什至尝试将 CreateNotification 的代码放入 RunOnUiThread - 没有结果。

因此,只有在我的 activity 中创建了该通知的情况下,才会触发通知的滑动关闭事件。但是我在收到来自 GCM 的消息时创建通知。它实际上会创建通知,即使应用程序不是 运行.

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

关于如何创建能够触发其滑动关闭事件的通知(在服务内部)有什么想法吗?

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

当你使用SetDeleteIntent(deleteIntent)方法时,确保你的deleteIntent是使用PendingIntent.GetBroadcast方法初始化的。

GetBroadcastet 表示检索将执行广播的 PendingIntent,如调用 Context.sendBroadcast()。当您使用 GetActivity 方法时,这意味着检索一个 PendingIntent 将启动一个新的 activity。这就是您的通知不会在 BroadcastReceiver 中触发 OnReceive 的原因。有关详细信息,您可以阅读此 document.

修改您的代码:

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

收件人:

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

然后在您的 GCM 服务中,您的通知的滑动关闭事件将被触发。