问题
I'm trying to open a fragment when a new Notification is received. For this I use a BroadcastReceiver. Problem is I'm not getting the message in my Activity from sendNotification
method inside FirebaseMessagingService
class. I think the issue is I declared the IntentFilter inside onCreate
and its not being called when I click on the push notification. Is there any other way to do this?
// Globally declared
private IntentFilter mIntentFilter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// Open a fragment automatically
if (findViewById(R.id.frame) != null) {
if (savedInstanceState != null) {
return;
}
myFragmentManager = getSupportFragmentManager();
myFragmentTransaction = myFragmentManager.beginTransaction();
myFragmentTransaction.replace(R.id.frame, new ProdList()).commit();
}
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.sam.CUSTOM_INTENT");
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.sam.CUSTOM_INTENT")) {
Toast.makeText(getApplicationContext(), "GOT MESSAGE", Toast.LENGTH_SHORT).show();
InventoryList il = new InventoryList();
replaceFragment(il,"IL");
}
}
};
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String value = entry.getValue();
try {
JSONObject obj = new JSONObject(value);
sendNotification(obj.getString("message"),"SUCCESS");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
private void sendNotification(String messageBody, String param) {
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction("MESSAGE");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_com.sam.CUSTOM_INTENTr)
.setContentTitle("Nika")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
回答1:
If you want BroadcastReceiver to get the broadcast, you have to actually send the Broadcast. Put this code in your onMessageReceived()
:
Intent intent = new Intent("com.sam.CUSTOM_INTENT");
context.sendBroadcast(intent);
This snippet will send the broadcast to all registered BroadcastReceivers with IntentFilter matching action "com.sam.CUSTOM_INTENT"
.
So, with current implementation, your activity will get broadcast when it is resumed.
来源:https://stackoverflow.com/questions/40211624/open-fragment-from-firebase-onmessagereceived