Android 7.1.1 Push-уведомления и FirebaseMessagingService не работают - PullRequest
0 голосов
/ 10 сентября 2018

Я занимаюсь разработкой приложения, которое работает с FirebaseMessagingService и NotificationCompat. Мой целевой API - 26. У меня проблема в том, что уведомления в android 7.1.1 появляются и закрываются сразу, они не сохраняются в панели уведомлений. Однако в Android 7.0 он работает отлично. Я видел много уроков, и во всех они делают то же самое, что и я. Я хотел бы знать, чего мне не хватает.

Мои услуги

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    Map data = remoteMessage.getData();

    if (SinchHelpers.isSinchPushPayload(data)) {
        //comentario                
    } else {            
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Message data payload => " + remoteMessage.getData());
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            try {
                sendNotification(object.get("message").toString(), object.get("chatId").toString(), object.get("user").toString());
                if (/* Check if data needs to be processed by long running job */ true) {
                    // For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
                    scheduleJob();
                } else {
                    // Handle message within 10 seconds
                    handleNow();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.d(TAG, "JSONException => " + e.getMessage());
            }

        }

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.d(TAG, "Message Notification Body => " + remoteMessage.getNotification().getBody());
        }

    }
}

}

private void sendNotification(String messageBody, String chatId, String userName) {
    try {
        Intent intent = new Intent(this, ChatActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("EXTRA_CHAT_ID", chatId);
        intent.putExtra("EXTRA_USER", userName);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);

        String channelId = "fcm_default_channel";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher_foreground)
                        .setContentTitle(userName)
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setGroup(userName)
                        .setSound(defaultSoundUri)
                        .setPriority(NotificationCompat.PRIORITY_MAX)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    } catch (Exception e) {
        Log.e(TAG, "userName => " + userName + " => " + e.getMessage());
    }
}

1 Ответ

0 голосов
/ 10 сентября 2018

Каналы уведомлений: Android 8.0 представляет каналы уведомлений, которые позволяют вам создавать настраиваемый пользователем канал для каждого типа уведомлений, которые вы хотите отобразить.

Пожалуйста, ознакомьтесь с документацией .

Создание уведомления .

...