-1

I am trying to start the startForegroundService() in android O and above devices.

And the service got started. In the onCreate() method of the service, I have added the startForeground() with notification.

But the notification is not coming. I cant able to see it.

My code in the onCreate() method of the service:

  Notification.Builder builder = new Notification.Builder(this, "1")
          .setContentTitle(getString(R.string.app_name))
          .setContentText("in app filling")
          .setAutoCancel(true);

  Notification notification = builder.build();
  startForeground(1, notification);
halfer
  • 19,824
  • 17
  • 99
  • 186
simple
  • 159
  • 1
  • 9

3 Answers3

0

Starting with Android O notifications should have the NotificationChannel specified, otherwise they won't be displayed and the error would appear in the log.

You can read more on notification channels here and on foreground services in Api 26+ here.

0

Solution:

Step1: Create a NotificationChannel

NotificationChannel notificationChannel = new NotificationChannel(channel_id , channel_name, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

Here, channel_id and channel_name are int and string variables respectively.

Step2: Attach it to the NotificationManager:

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

Step3: Create your Notification:

NotificationCompat.Builder notification = new NotificationCompat.Builder(this, "channel_id")
                        .setContentTitle("Test Title")
                        .setContentText("Test Message")
                        .setSmallIcon(R.mipmap.ic_launcher);

Step4: Attach the notification in the same NotificationManager object

notificationManager.notify(1, notification.build());

Finally, put up a check to notify if it's above Android O:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    ....
}

Referred and More about this can be found Here

Hope it helps.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
0

From Android version Oreo, You must have to add the channel to your notification like this:

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) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

and create object for notification like this:

Notification.Builder notification = new Notification.Builder(this, "CHANNEL_ID")
Eren Utku
  • 1,731
  • 1
  • 18
  • 27
Erselan Khan
  • 692
  • 6
  • 13