V7.app.NotificationCompat无法在Oreo上运行

时间:2018-08-24 12:27:09

标签: android android-notifications

我是Android的新手,我必须做一个Music Player。我从github举了一个例子,我正在实现我所需要的东西,但是当我将代码从版本25.0.0更新为27.0.3时,它给了我一个关于notification的错误:

  

android.app.RemoteServiceException:startForeground的错误通知:java.lang.RuntimeException:服务通知的无效通道:Notification(channel = null pri = 0 contentView = null振动= null声音= null默认值= 0x0标志= 0x40颜色= 0xffd85840 category = transport actions = 3 vis = PUBLIC)

我试图找到解决方案,但失败了。

这是我的代码:

public class PlayingNotificationImpl24 implements PlayingNotification {

private static final int NOTIFY_MODE_FOREGROUND = 1;
private static final int NOTIFY_MODE_BACKGROUND = 0;

private MusicService service;

private NotificationManager notificationManager;

private int notifyMode = NOTIFY_MODE_BACKGROUND;

private boolean stopped;

@Override
public synchronized void init(MusicService service) {
    this.service = service;
    notificationManager = (NotificationManager) service.getSystemService(NOTIFICATION_SERVICE);
}

@Override
public synchronized void update() {
    stopped = false;

    final Song song = service.getCurrentSong();

    final String albumName = song.albumName;
    final String artistName = song.artistName;
    final boolean isPlaying = service.isPlaying();
    final String text = TextUtils.isEmpty(albumName)
            ? artistName : artistName + " - " + albumName;

    final int playButtonResId = isPlaying
            ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;

    Intent action = new Intent(service, MainActivity.class);
    action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final PendingIntent clickIntent = PendingIntent.getActivity(service, 0, action, 0);

    final ComponentName serviceName = new ComponentName(service, MusicService.class);
    Intent intent = new Intent(MusicService.ACTION_QUIT);
    intent.setComponent(serviceName);
    final PendingIntent deleteIntent = PendingIntent.getService(service, 0, intent, 0);

    final int bigNotificationImageSize = service.getResources().getDimensionPixelSize(R.dimen.notification_big_image_size);
    service.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            SongGlideRequest.Builder.from(Glide.with(service), song)
                    .checkIgnoreMediaStore(service)
                    .generatePalette(service).build()
                    .into(new SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize, bigNotificationImageSize) {
                        @Override
                        public void onResourceReady(BitmapPaletteWrapper resource, GlideAnimation<? super BitmapPaletteWrapper> glideAnimation) {
                            Palette palette = resource.getPalette();
                            update(resource.getBitmap(), palette.getVibrantColor(palette.getMutedColor(Color.TRANSPARENT)));
                        }

                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            update(null, Color.TRANSPARENT);
                        }

                        void update(Bitmap bitmap, int color) {
                            if (bitmap == null)
                                bitmap = BitmapFactory.decodeResource(service.getResources(), R.drawable.default_album_art);
                            NotificationCompat.Action playPauseAction = new NotificationCompat.Action(playButtonResId,
                                    service.getString(R.string.action_play_pause),
                                    retrievePlaybackAction(MusicService.ACTION_TOGGLE_PAUSE));
                            NotificationCompat.Action previousAction = new NotificationCompat.Action(R.drawable.ic_skip_previous_white_24dp,
                                    service.getString(R.string.action_previous),
                                    retrievePlaybackAction(MusicService.ACTION_REWIND));
                            NotificationCompat.Action nextAction = new NotificationCompat.Action(R.drawable.ic_skip_next_white_24dp,
                                    service.getString(R.string.action_next),
                                    retrievePlaybackAction(MusicService.ACTION_SKIP));
                            NotificationCompat.Builder builder = new NotificationCompat.Builder(service)
                                    .setSmallIcon(R.drawable.sp_logo)
                                    .setLargeIcon(bitmap)
                                    .setContentIntent(clickIntent)
                                    .setDeleteIntent(deleteIntent)
                                    .setContentTitle(song.title)
                                    .setContentText(text)
                                    .setOngoing(isPlaying)
                                    .setShowWhen(false)
                                    .addAction(previousAction)
                                    .addAction(playPauseAction)
                                    .addAction(nextAction);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                builder.setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle().setMediaSession(service.getMediaSession().getSessionToken()).setShowActionsInCompactView(0, 1, 2))
                                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                                if (PreferenceUtil.getInstance(service).coloredNotification())
                                    builder.setColor(color);
                            }

                            if (stopped)
                                return; // notification has been stopped before loading was finished
                            updateNotifyModeAndPostNotification(builder.build());
                        }
                    });
        }
    });
}

private PendingIntent retrievePlaybackAction(final String action) {
    final ComponentName serviceName = new ComponentName(service, MusicService.class);
    Intent intent = new Intent(action);
    intent.setComponent(serviceName);

    return PendingIntent.getService(service, 0, intent, 0);
}

private void updateNotifyModeAndPostNotification(Notification notification) {
    int newNotifyMode;
    if (service.isPlaying()) {
        newNotifyMode = NOTIFY_MODE_FOREGROUND;
    } else {
        newNotifyMode = NOTIFY_MODE_BACKGROUND;
    }

    if (notifyMode != newNotifyMode && newNotifyMode == NOTIFY_MODE_BACKGROUND) {
        service.stopForeground(false);
    }

    if (newNotifyMode == NOTIFY_MODE_FOREGROUND) {
        service.startForeground(NOTIFICATION_ID, notification);
    } else if (newNotifyMode == NOTIFY_MODE_BACKGROUND) {
        notificationManager.notify(NOTIFICATION_ID, notification);
    }

    notifyMode = newNotifyMode;
}

@Override
public synchronized void stop() {
    stopped = true;
    service.stopForeground(true);
    notificationManager.cancel(NOTIFICATION_ID);
}
}



compileSdkVersion 27
buildToolsVersion '27.0.3'
targetSdkVersion 27

1 个答案:

答案 0 :(得分:2)

您没有注册NotificationChannel。在Android 8.0及更高版本上传递通知之前,您必须通过将NotificationChannel的实例传递到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);
    }

您必须先创建通知频道,然后才能在Android 8.0及更高版本上发布任何通知,您应在应用启动后立即执行此代码。建议您访问the docs了解更多详细信息。