3

Does not show notification on Android 9.0 and works great on 8 and below. I had trouble with old code and I read that is needed to pass Channel on new android version, I got this code from internet but still is not working on Android 9.0. I don't know why. Any help will be appreciated.

As I read through internet everything looks good. But notification does not show.

    private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
        String CHANNEL_ID = Constants.ChannelID;
        String CHANNEL_NAME = "Notification";

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);;
        NotificationCompat.Builder notification;
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        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.notification),
                    new AudioAttributes.Builder()
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .build());
            channel.canShowBadge();
            notificationManager.createNotificationChannel(channel);
            notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
        } else {
            notification = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
        }
        notification
                .setVibrate(new long[]{0, 100})
                .setPriority(Notification.PRIORITY_MAX)
                .setLights(Color.BLUE, 3000, 3000)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .setWhen(getTimeMilliSec(timeStamp))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(inboxStyle)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
                .setContentText(message)
                .build();

        notificationManager.notify(CHANNEL_ID, 1, notification.build());
    }
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
Web.11
  • 406
  • 2
  • 8
  • 23

3 Answers3

2

I think the problem is you did not setChannelId() on the notificationBuilder. (I took the liberty to rename notification to notificationBuilder since that is what it is.)

I removed an superfluous semi-colons in one of the lines of code.

Also note that canShowBadge() is a class method that returns a value, but does not set any notification settings. I replaced it with setShowBadge(true).

Please read the comments I left in the code fragment for additional information.

private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
        String CHANNEL_ID = Constants.ChannelID;
        String CHANNEL_NAME = "Notification";

        // I removed one of the semi-colons in the next line of code
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

        inboxStyle.addLine(message);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
            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.notification),
                    new AudioAttributes.Builder()
                            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                            .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                            .build());
            //channel.canShowBadge();
            // Did you mean to set the property to enable Show Badge?
            channel.setShowBadge(true);
            notificationManager.createNotificationChannel(channel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
            .setVibrate(new long[]{0, 100})
            .setPriority(Notification.PRIORITY_MAX)
            .setLights(Color.BLUE, 3000, 3000)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentIntent(resultPendingIntent)
            .setWhen(getTimeMilliSec(timeStamp))
            .setSmallIcon(R.mipmap.ic_launcher)
            .setStyle(inboxStyle)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
            .setContentText(message);
        // Removed .build() since you use it below...no need to build it twice

        // Don't forget to set the ChannelID!!
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            notificationBuilder.setChannelId(ID_SPECIAL_OFFER_NOTIFICATION);
        }

        notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
    }
Barns
  • 4,850
  • 3
  • 17
  • 31
0

Yes this already answered but help out to needfull

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int returnFlag = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
            Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("Mytrux Running")
                    .setAutoCancel(true);
            Notification notification = builder.build();
            startForeground(NOTIFICATION_ID, notification);
            returnFlag = START_NOT_STICKY;
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("Mytrux is Running...")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setAutoCancel(true);
            Notification notification = builder.build();
            startForeground(NOTIFICATION_ID, notification);
            returnFlag = START_STICKY;
        }
        mHandlerTask.run();
        return returnFlag;
    }

and add this method also

private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    ANDROID_CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

don't forget to add service in the manifest.

CrazyMind
  • 1,006
  • 1
  • 20
  • 22
0

Okay this code now works for me, and also teseted until android 12 First I create Channel

// Create Channel For Notifications (if does not exist)
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
        NotificationChannel existingChannel = mNotificationManager.getNotificationChannel(Constants.notification_channel_ID);
        // If channel does not exist then create.
        if (existingChannel == null) {
            CharSequence name = getResources().getString(R.string.app_name);
            String description = getResources().getString(R.string.notification_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel( Constants.notification_channel_ID , name , importance) ;
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true) ;
            notificationChannel.setLightColor(Color.BLUE) ;
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
}

Maybe is not needed to check if channel exists but I check. Then when I need to show notification I call this method:

private void notification(){
     Intent intent = new Intent(this, MainActivity.class);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     PendingIntent pendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    
     NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Constants.notification_channel_ID)
                                .setChannelId(Constants.notification_channel_ID)
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setContentTitle(this.getResources().getString(R.string.app_name))
                                .setContentText(this.getResources().getString(R.string.description))
                                .setContentIntent(pendingIntent)
                                .setAutoCancel(true);
      NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
      notificationManager.notify(Constants.NOTIFICATION_ID, builder.build());
}
Web.11
  • 406
  • 2
  • 8
  • 23