Я пытаюсь открыть фрагмент, когда получено новое уведомление. Для этого я использую BroadcastReceiver. Проблема в том, что я не получаю сообщение в своей деятельности от sendNotification
метода внутри класса FirebaseMessagingService
. Я думаю, что проблема заключается в том, что я объявил IntentFilter внутри onCreate
и его не вызывается, когда я нажимаю на push-уведомление. Есть ли другой способ сделать это?открытый фрагмент из Firebase onMessageReceived
// Globally declared
\t private IntentFilter mIntentFilter;
\t
\t @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
\t \t
\t \t // Open a fragment automatically
\t \t 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");
}
\t
\t @Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
\t
\t 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();
}
}
}
}
\t
\t
\t 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);
\t \t 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());
}
Почему вы использовали широковещательный приемник внутри самой домашней деятельности? Вы уже приходите в HomeActivity, когда уведомление будет нажато правильно? – Jai
Cuz Мне нужно открыть фрагмент, используя FrameLayout, который находится в макете HomeActivity. –
, но вы уже меняете метод oncreate, а затем зачем нужен широковещательный приемник? – Jai