Как добавить уведомление музыкального проигрывателя в API 26 - PullRequest
0 голосов
/ 08 июня 2018

Мне удалось показать уведомление, но проблема в том, что он работает не так, как ожидалось.Проблемы в том, что: 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 голосов
/ 08 июня 2018

При создании канала уведомлений установите высокую важность уведомления NotificationManger.IMPORTANCE_HIGH

  NotificationChannel channel = new NotificationChannel(channelId, ChannelName, NotificationManager.IMPORTANCE_HIGH);
0 голосов
/ 08 июня 2018

Согласно https://developer.android.com/training/notify-user/channels,

1) Приоритет уведомления можно установить только с помощью NotificationManager .IMPORTANCE_HIGH 2) Необходимо знать Идентификатор для других каналов музыкальных приложений CHANNEL_ID, может быть, этот пост поможет вам: https://medium.com/google-developers/migrating-mediastyle-notifications-to-support-android-o-29c7edeca9b7

...