Android IntentService无法启动通知

时间:2020-01-30 04:51:20

标签: java android

这是我的full code

开始通知在Service中工作正常,但是当我将服务更改为IntentService时,通知未显示,这是我的服务:

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

我使用以下代码启动服务:

startService(new Intent(this, MyIntentService.class))

1 个答案:

答案 0 :(得分:0)

我找到了解决方法

IntentService无法使用startforeground,因为IntentService将在onHandleIntent之后停止,因此无法长时间进行前台通知(请参见StartForeground for IntentService

如果我想使用startForeground,请使用Service代替IntentSerivce

如果我想在IntentService中使用通知,请使用NotificationManager#notify,请参见以下代码:

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