@newdancer

Почему могут не срабатывать push уведомления если приложение активно?

Почему могут не срабатывать push уведомления если приложение активно?
Хотя в onMessageReceived они приходят но почему то не срабатывают! Как с этим бороться?
Сам код сервиса п обработке пушей
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);
        }
    }
}
  • Вопрос задан
  • 126 просмотров
Пригласить эксперта
Ответы на вопрос 1
@ivanessence
Android Developer
На других версиях Android работает? Если onMessageReceived отрабатывает - возможно не корректно написан код обработки пушей и их отображение.

Если приложение закрыто, то пуши обрабатывает не код Вашего приложения, а сама система Android.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы