Android точка значка уведомления не отображается для пользовательского firebase pu sh уведомление - PullRequest
1 голос
/ 19 июня 2020

Я реализовал настраиваемый вид для уведомлений firebase pu sh. Для настраиваемого представления нам нужно удалить ключ «уведомление» из pu sh Json, чтобы его можно было обрабатывать, даже когда приложение закрыто, как показано ниже:

{  
  "data": {
    "detail": {
     }
  },
  "to": "" 
}

Для создания настраиваемого уведомления я использовал код ниже :

private void generateNotification(String title, String message, Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        String channelId = getString(R.string.default_notification_channel_id);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, notificationCount, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        /*
        * Custom notification layout
        * */
        String notificationHeaderText = getResources().getString(R.string.app_name) + " \u2022 "
                + DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME);
        RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
        collapsedView.setTextViewText(R.id.timestamp, notificationHeaderText);
        collapsedView.setTextViewText(R.id.content_title, title);
        collapsedView.setTextViewText(R.id.content_text, message);

        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
        expandedView.setTextViewText(R.id.timestamp, notificationHeaderText);
        expandedView.setTextViewText(R.id.content_title, title);
        expandedView.setTextViewText(R.id.notification_message, message);

        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.footer_click);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                .setSmallIcon(R.drawable.ic_inkclick_logo_colored)
                .setSound(soundUri)
                .setGroup(GROUP_KEY_INKCLICK)
                .setAutoCancel(true)
                .setGroupSummary(true)
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (manager != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        getResources().getString(R.string.default_notification_channel_id), NotificationManager.IMPORTANCE_HIGH);
                channel.enableLights(true);
                channel.setLightColor(Color.MAGENTA);
                channel.setVibrationPattern(new long[]{0, 1000/*, 500, 1000*/});
                channel.enableVibration(true);
                channel.setShowBadge(true);
                AudioAttributes audioAttributes = new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build();
                channel.setSound(soundUri, audioAttributes);
                manager.createNotificationChannel(channel);
            }
            manager.notify(0, notificationBuilder.build());
        }
        else {
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.notify(0, notificationBuilder.build());
            }

        }
        notificationCount += 1;
    }

Я добавил channel.setShowBadge(true); после прочтения официальной документации по значку уведомлений здесь , а также других ответов на stackoverflow, например this и this .

Я также пытался удалить приложение и перезапустить устройство, но значок не отображается. Устройство работает по API 28 (P ie).

...