2

I have implemented custom view for firebase push notification. For custom view we need to remove "notification" key from push Json so that it can be handled even when app is closed like below:

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

For creating custom notification I used below code:

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;
    }

I have added channel.setShowBadge(true); after reading the official notification badge documentation here as well as other answers on stackoverflow like this and this.

I have also tried uninstalling the app and restarting the device but the badge is not showing. Device is running on API 28(Pie).

  • Did you have any luck getting this to work? I am running into the same issue. The notification itself shows up fine, but no matter what I try, that notification dot will not appear. – etrado Jul 31 '20 at 18:12
  • @etrado I haven't found any solution yet. Still looking for answers. – Chinmay Bhardwaj Aug 02 '20 at 07:21

3 Answers3

0

You can set the style of the Android notification badge like this:

.setStyle(new NotificationCompat.DecoratedCustomViewStyle())

The overall code can look like this:

  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)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
0

You can set style by adding below line

.setStyle(new NotificationCompat.DecoratedCustomViewStyle())

add this code

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)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) 
                .setCustomContentView(collapsedView)
                .setCustomBigContentView(expandedView)
                .setContentIntent(pendingIntent);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Mr Abbasi
  • 97
  • 11
0

You need to specify the title or text (setContentTitle or setContentText). Because Android SDK uses it to describe notification in the window which appears when you long-click to the app icon. I don't know why this is not specified in the documentation, but it works this way.

Lon-click notification window

BoP
  • 2,310
  • 1
  • 15
  • 24
Filfur
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Dec 21 '21 at 12:45