Ad

Android Notification Does Not Show

guys I used this code to show notification, but today I tried to create a new application, and does not show notification, I have no clue what is going on. I send notification from firebase, and I don't show notification. Any idea ?

On emulator it show notification, and not in my Samsung A8 (2018), I have same code in other app it works great.

  private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent, Uri alarmSound) {

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Constants.TOPIC_GLOBAL);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        Notification notification;
        notification = builder
                .setChannelId(Constants.TOPIC_GLOBAL)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setSound(alarmSound)
                .setStyle(inboxStyle)
                .setWhen(getTimeMilliSec(timeStamp))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
                .setContentText(message)
                .build();

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(100, notification);
    }
Ad

Answer

 String CHANNEL_ID = "1";
    String CHANNEL_NAME = "Notification";

    Notification.Builder notification;

    if (Build.VERSION.SDK_INT >= 26) {
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.enableVibration(true);
        channel.setLightColor(Color.BLUE);
        channel.enableLights(true);
        channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.trial),
                new AudioAttributes.Builder()
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                        .build());
        channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
        notificationManager.createNotificationChannel(channel);
        notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID);
    } else {
        notification = new Notification.Builder(getApplicationContext());
        notification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.trial));
    }
    notification.setContentTitle(Title)
            .setVibrate(new long[]{0, 100})
            .setPriority(Notification.PRIORITY_MAX)
            .setContentText(ContentText)
            .setColor(ContextCompat.getColor(getApplicationContext(), R.color.green))
            .setLights(Color.RED, 3000, 3000)
            .setSmallIcon(R.drawable.ic_notifications_active_purple_24dp)
            .setContentIntent(pendingIntent)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true);

    notificationManager.notify(CHANNEL_ID, 1, notification.build());
Ad
source: stackoverflow.com
Ad