如何在API 26中添加音乐播放器通知

时间:2018-06-08 17:03:33

标签: java android android-studio notifications android-8.0-oreo

我已设法显示通知,但问题是这没有按预期工作。问题是 - 1.我不知道如何将通知保持在最顶层 2.当我滑动通知时,它会显示贪睡图标,但不会显示在任何其他音乐应用中。

这是我的代码

`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    RemoteViews expandedView = new RemoteViews(getContext().getPackageName(), R.layout.custom_push);
                    CharSequence name = "Music Player";
                    String description = "Play music in background";
                    int importance = NotificationManager.IMPORTANCE_LOW;
                    NotificationChannel channel = new NotificationChannel("Music Player", name, importance);
                    channel.setDescription(description);
                    channel.setShowBadge(false);
                    // Register the channel with the system; you can't change the importance
                    // or other notification behaviors after this
                    NotificationCompat.Builder nc = new NotificationCompat.Builder(getContext(),"Music Player");
                    Intent notifyIntent = new Intent(getContext(), now_playing.class);

                    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
                    nc.setContentIntent(pendingIntent);
                    nc.setCustomBigContentView(expandedView);
                    nc.setSmallIcon(R.drawable.notification_icon);
                    nc.setAutoCancel(false);
                    nc.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                    nc.setOngoing(true);
                    NotificationManager notificationManager = getActivity().getSystemService(NotificationManager.class);
                    notificationManager.createNotificationChannel(channel);
                    notificationManager.notify(1, nc.build());
                }`

另外我注意到其他音乐播放器在设置中没有任何通知频道。如果有人告诉我如何正确实施通知,那将会很棒。 的由于

2 个答案:

答案 0 :(得分:0)

根据https://developer.android.com/training/notify-user/channels

1)只能使用CHANNEL_ID设置通知的优先级 2)有必要知道其他音乐应用频道send_event的标识符,也许这篇文章可以帮助您:https://medium.com/google-developers/migrating-mediastyle-notifications-to-support-android-o-29c7edeca9b7

答案 1 :(得分:0)

创建通知通道设置通知重要性高NotificationManger.IMPORTANCE_HIGH

  NotificationChannel channel = new NotificationChannel(channelId, ChannelName, NotificationManager.IMPORTANCE_HIGH);