当前位置: 首页 > 知识库问答 >
问题:

V7.app.NotificationCompat在Oreo上不起作用

罗华翰
2023-03-14

我是Android的新手,我必须制作一个音乐播放器。我从github中举了一个例子,我正在实现我需要的东西,但是当我将代码从25.0.0更新到27.0.3时,它会给我一个关于通知的错误:

Android.app.remoteserviceException:StartForeground的错误通知:java.lang.runtimeException:服务通知的无效通道:通知(通道=null pri=0 contentview=null振动=null sound=null defaults=0x0 flags=0x40 color=0xfd85840 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个答案

谭正谊
2023-03-14

您没有注册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及更高版本上发布任何通知之前,您必须创建通知通道,您应该在应用程序启动时立即执行此代码。我建议你去查阅文件以了解更多的细节。

 类似资料:
  • 有人能帮我解决这个问题吗?

  • 我有一种字体在IE8和IE9以及所有其他浏览器中都能正常工作,但第二种字体只在IE9和其他浏览器中工作。在IE8中,我得到了一个错误: “CSS3111:@font-face遇到未知错误。” 我一直在四处看书,尝试了很多事情,但都没有运气。 我的代码是: 我试过用几个不同的生成器多次重新生成eot,玩过CSS,玩过头,玩过域,等等,但是看起来它们都很好。如前所述,相同的代码在相同的,只是指向不同的

  • 我实现了spring Security3.2.5,但不幸的是@preauthorize不能用于类和方法。正如我从文档中读到的,@preauthorize应该允许方法和类工作,如果用户在注释中有指定的角色,但我能够运行所有方法或类,而没有任何角色差异。您可以看到security-config.xml和security.context.xml以及我在下面声明@preauthorize注释的类。如果你能

  • 我在Oreo8中打开webview时出现了这个错误。该设备是谷歌Pixel。请帮我做同样的事。如果您需要任何其他信息,请让我知道。 process:com.android.chrome:sandboxed_process0,PID:16787 Android.content.res.resources.obtaintypedarray(Resources.java:617)在Android.con

  • 问题内容: 我正在尝试使形状的动画随着样本大小的增加(从100增加到1000)而呈指数分布。还有一个滑块,用于配置发行版的lambda参数,以及一个用于启动动画的按钮。 我的问题是,即使布局得到渲染,当我按下开始按钮时也没有动画发生。到目前为止,这是我的尝试: 为了简化起见,我在Python笔记本中运行它: 问题答案: 您应该注意函数的名称空间。 考虑 因此,您需要处理实际对象,而不是它们的某些本

  • 我的代码没有验证select。怎么了?。这个问题的解决方案是什么?很抱歉我的英语不好,非常感谢你的回答 这是我的表格 我的Javacript 忽略它:Lorem ipsum dolor sit amet,奉献精英。毛里斯·塞德·里索斯和莫利斯·迪克特。在faucibus中,Interdum和malesuada在第一次同侧前就出名了。我是一个爱的人。我是帕特·泰勒斯,我是康格·奈克。南阿罗特鲁姆尼斯