android - 关闭来自 BroadCastReceiver 的通知

标签 android notifications broadcastreceiver google-cloud-messaging

我在 Android 应用程序的主要 Activity 中有一个子类 BroadCastReceiver,用于在 GCM 进入时处理 GUI 的更改。所有这些都工作正常。

唯一不起作用的是,如果调用此方法,则用户会在他的设备上打开我的应用程序,因此我想清除我输入到此传入 GCM 的通知中心的通知。如果我将 cancelAll() 和 cancel(int) 行放入我的 GCMIntentService 中,那么它会消除通知,但我无法将其保留在那里,因为我只想在用户打开我的应用程序时消除此通知。我使用了 cancel() 和 cancelAll() 来确保我没有以某种方式从 IntentService 传递错误的通知 ID(我已经验证它是正确的)。

问题是没有错误,通知中心的通知根本不会消失。自从我在 logcat 中获取日志条目以来,我已验证该方法正在被调用。

我引用了之前的答案,但由于某种原因它在我的实现中不起作用:Starting and stopping a notification from broadcast receiver

我的主要 Activity 中的代码:

private class NotificationBroadcastReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    Log.v("Push", "Received Push Notification!");

    //Now that the user has opened the app, cancel the notification in the notification center if it is still there
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    final int NOTIFICATION_ID = intent.getIntExtra("notificationId",0);

    Log.v("NOTIFICATION_ID",Integer.toString(NOTIFICATION_ID));

    mNotificationManager.cancel(NOTIFICATION_ID);
    mNotificationManager.cancelAll();

    Log.v("Canceled Notifs","Canceled");
  }
} 

有什么想法吗?

最佳答案

一个可能更好的选择是,不要在应用程序打开时发布通知,而是首先检测应用程序是否可见,如果可见,则不显示通知。您可以使用 Intent 广播或 EventBus 或 Otto 等事件总线来完成此操作。

示例:

public class GCMReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // this is your receiver after gcm has been received 
        Intent appOpenCheck = new Intent("com.example.IS_APP_OPEN");
        // put extras about your notification here...
        context.sendOrderedBroadcast(appOpenCheck);
    }
}

public class AppNotOpenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // this is your receiver that will be hit if the app isn't open 
        // post your notification to the NotificationManager
    }
}

然后,在 AndroidManifext.xml 中:

<receiver
    android:name=".AppNotOpenReceiver">
         <intent-filter>
            <action android:name="com.example.IS_APP_OPEN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>

最后,在您的Activity中:

BroadcastReceiver appOpenAlert = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        abortBroadcast(); // prevents notification from going further
        // this is your receiver that will be hit if the app is open 
        // update your ui
    }
}

@Override
public void onStart() {
    super.onStart();
    IntentFilter notificationReceived = new IntentFilter();
    notificationReceived.addAction("com.example.IS_APP_OPEN");
    notificationReceived.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    registerReceiver(appOpenAlert, notificationReceived);
}

@Override
public void onStop() {
    unregisterReceiver(appOpenAlert);
    super.onStop();
}

关于android - 关闭来自 BroadCastReceiver 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25294264/

相关文章:

android - 带有关闭通知操作按钮的多重通知

android - Android "id"广播 Intent 中的 "Google Play Music"字段对应什么?

android - 如何从 Android 上保存的帐户中检索 Facebook-AuthToken

swift - 当应用程序关闭时,通知 willPresent 不会调用

java - 无法创建数据库

android - Trigger.io 通知 : forge. notification.showLoading() 不适用于 Android

android - 创建显示为拼图的 Android 通知

android - 无法从传入消息中获取广播接收

android - 如何在 Android 中缩放路径?

Android JsonObject 到数组示例