Answer the question
In order to leave comments, you need to log in
Why push notifications may not work if the application is active?
Why push notifications may not work if the application is active?
Although they come to onMessageReceived, but for some reason they do not work! How to deal with it?
The code of the service for processing push notifications
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static String ADMIN_CHANNEL_ID = "PUSH_MESS";
private NotificationManager mNotificationManager = null;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels();
}
int notificationId = new Random().nextInt(60000);
sendNotification(remoteMessage, notificationId);
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
Log.d("TEST", "FCM onDeletedMessages");
}
public void sendNotification(RemoteMessage remoteMessage, int notificationId) {
PendingIntent contentIntent;
String route = "";
String type = "";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
String key = entry.getKey();
if (key.equalsIgnoreCase(AppConstants.PUSH_ACTION_ROUTE))
route = entry.getValue();
if (key.equalsIgnoreCase(AppConstants.PUSH_ACTION_TYPE))
type = entry.getValue();
}
Log.d("TEST", "route=" + route + " type=" + type);
if (!TextUtils.isEmpty(type)) {
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.putExtra(AppConstants.PUSH_ACTION_ROUTE, route);
notificationIntent.putExtra(AppConstants.PUSH_ACTION_TYPE, type);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder nb = new NotificationCompat.Builder(getApplicationContext(), "push_default")
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setNumber(1)
.setColor(255)
.setChannelId(AppConstants.PUSH_CHANNEl_ID)
.setContentIntent(contentIntent)
.setWhen(System.currentTimeMillis());
mNotificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, nb.build());
if (CommonUtils.isAppRunning(getApplicationContext())) {
Prefs.save(getApplicationContext(), Prefs.PREFS_PUSH_ROUTE, route);
Prefs.save(getApplicationContext(), Prefs.PREFS_PUSH_TYPE, type);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels() {
CharSequence adminChannelName = getString(R.string.notifications_admin_channel_name);
String adminChannelDescription = getString(R.string.notifications_admin_channel_description);
NotificationChannel adminChannel;
adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW);
adminChannel.setDescription(adminChannelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (mNotificationManager != null) {
mNotificationManager.createNotificationChannel(adminChannel);
}
}
}
Answer the question
In order to leave comments, you need to log in
Does it work on other Android versions? If onMessageReceived works, the code for processing pushes and displaying them is probably not written correctly.
If the application is closed, then pushes are processed not by your application code, but by the Android system itself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question