0

here is my full code

Start notification work well in Service, but when i change service to IntentService, the notification doesn't show, here is my service:

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }

    Context ctx;
    String channelId;

    @Override
    protected void onHandleIntent(Intent intent) {
        ctx = getApplicationContext();
        channelId = getPackageName() + " Channel";
        createNotificationChannel();
        startForeground(1, getNotification());
    }

    Notification getNotification() {
        return new NotificationCompat.Builder(ctx, channelId)
                .setContentTitle("Title")
                .setContentText("Content")
                .setSmallIcon(R.drawable.ic_android_black_24dp)
                .build();
    }

    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) {
            String name = channelId + " Name";
            String description = channelId + " Desc";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, 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);
        }
    }
}

I start service with following code:

startService(new Intent(this, MyIntentService.class))
chikadance
  • 3,591
  • 4
  • 41
  • 73
  • ["it doesn't work" is unhelpful](http://idownvotedbecau.se/itsnotworking/). For instance: is the service code getting executed and the notification isn't posting? Is the service code not getting executed at all? We need more info. – Ryan M Jan 30 '20 at 04:57
  • Also that permission is totally unrelated to this - do you have the correct permissions? – Ryan M Jan 30 '20 at 04:59

1 Answers1

0

I find the solution

IntentService cannot use startforeground since IntentService will stop after onHandleIntent, it cannot long-time foreground notification(see StartForeground for IntentService)

If I wanna use startForeground, use Service instead IntentSerivce

If I wanna use notification in IntentService, use NotificationManager#notify, see following code:

public class MyIntentService extends IntentService {
    public MyIntentService() {
        super("MyIntentService");
    }

    Context ctx;
    String channelId;

    @Override
    protected void onHandleIntent(Intent intent) {
        initNotification();
    }

    private void initNotification() {
        ctx = getApplicationContext();
        channelId = getPackageName() + " Channel";
        createNotificationChannel();
//        startForeground(1, getNotification());
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(1, getNotification());
    }

    Notification getNotification() {
        return new NotificationCompat.Builder(ctx, channelId)
                .setContentTitle("Title")
                .setContentText("Content")
                .setSmallIcon(R.drawable.ic_android_black_24dp)
                .build();
    }

    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) {
            String name = channelId + " Name";
            String description = channelId + " Desc";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(channelId, 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);
        }
    }
}
chikadance
  • 3,591
  • 4
  • 41
  • 73