我正在使用FCM,在onMessageReceived()
中我正在使用PendingIntent.getBroadcast()
调用同一服务中存在的BroadcastReceiver
。在下面的代码中,如果从通知托盘中删除了通知,我将发送广播。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
......
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendingIntent);
......
}
此外,在通过其他活动发送广播时,我正在使用onCreate()
方法注册BroadcastReceiver
。
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
// registering the broadcast receiver on action "broadcastReceiverAction"
IntentFilter intentFilter = new IntentFilter("broadcastReceiverAction");
LocalBroadcastManager.getInstance(this).registerReceiver(new MyBroadcastReceiver(), intentFilter);
}
我观察到,从清单中删除静态接收器时,尽管已将广播接收器注册在BrodcastReceiver
中,但仍未调用onCreate()
来删除通知。 PendingIntent.getBroadcast()
方法。为什么会这样呢?这是否意味着public static class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences sharedPreferences = context.getSharedPreferences("shared", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(String.valueOf(intent.getExtras().getInt("id")));
editor.commit();
}
}
需要一个静态接收器?
MyBroadcastReceiver
<receiver android:name=".MyFirebaseMessagingService$MyBroadcastReceiver" android:exported="true">
<intent-filter>
<action android:name="broadcastReceiverAction" />
</intent-filter>
</receiver>
编辑1 静态广播接收器
onDestroy()
在@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
// un-registering receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(new MyBroadcastReceiver());
}
上取消广播接收器的注册
google_services.json
答案 0 :(得分:0)
因为您的代码使用动态广播(非静态)是不正确的。
首先,您以onCreate
方法注册
// registering the broadcast receiver on action "broadcastReceiverAction"
IntentFilter intentFilter = new IntentFilter("broadcastReceiverAction");
LocalBroadcastManager.getInstance(this).registerReceiver(new MyBroadcastReceiver(), intentFilter);
然后您发送广播
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
您忘记将action
添加到由PendingIntent包裹的意图中,该意图说明系统为何找不到并触发您的广播。
将代码更改为
Intent intent = new Intent(getApplicationContext(), MyBroadcastReceiver.class);
intent.setAction("broadcastReceiverAction"); // Add this line
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
更新:删除通知后,PendingIntent
将执行广播,就像呼叫Context.sendBroadcast()
一样。在您的代码中,您使用LocalBroadcastManager
,该捕获的message/intent
仅从应用程序的组件中广播。它无法从系统中捕获message/intent
。
因此将代码更改为
private MyBroadcastReceiver myBroadcastReceiver;
@Override
public void onCreate() {
super.onCreate();
// registering the broadcast receiver on action "broadcastReceiverAction"
IntentFilter intentFilter = new IntentFilter("broadcastReceiverAction");
myBroadcastReceiver = new MyBroadcastReceiver();
registerReceiver(myBroadcastReceiver, intentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
......
Intent intent = new Intent("broadcastReceiverAction");
intent.putExtra("id", NOTIFICATION_ID);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setDeleteIntent(pendingIntent);
......
}