Средства управления мультимедиа не работают на Oreo - PullRequest
0 голосов
/ 04 апреля 2020

Я пытался создать медиа-уведомление с элементами управления, но я застрял при попытке заставить кнопки работать. Я реализовал широковещательный приемник, который должен принимать сигнал и выполнять задачи pause / play / next / previous, но каким-то образом широковещательный приемник не вызывается. Вот мой код:

 private void showNotification(int notificationId, String title, String message, String channelId, String reminderName, int openType, ArrayList<NotificationCompat.Action> actions, Bitmap... bitmap) {
    Intent intent = new Intent(mContext, MainActivity.class);
    MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, "tag");

    intent.putExtra("fromPush", true);
    Log.e("fffff", String.valueOf(notificationId));
    intent.putExtra(Const.NOTIFICATION_ID, notificationId);


    //Start IntentNotification
    Intent notificationIntent = new Intent(mContext, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);


    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);


    //With this settings you can open the same Activity without recreate.
    //But you have to put in your AndroidManifest.xml the next line: to your Activity
    //activity android:name=".MainActivity" android:launchMode="singleInstance"


    //Intent for Play
    Intent dI = new Intent(mContext, AudioStreamingManager.class);
    dI.setAction(Constants.ACTION.DISLIKE_ACTION);
    PendingIntent dislikeIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 1, dI, PendingIntent.FLAG_CANCEL_CURRENT);

    //Intent for Play
    Intent pI = new Intent(mContext, AudioStreamingManager.class);
    pI.setAction(Constants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 1, pI, PendingIntent.FLAG_CANCEL_CURRENT);

    //Intent for Pause
    Intent pauInt = new Intent(mContext, AudioStreamingManager.class);
    pauInt.setAction(Constants.ACTION.PAUSE_ACTION);
    PendingIntent pauseIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 2, pauInt, PendingIntent.FLAG_CANCEL_CURRENT);

    //Intent for Close
    Intent stopIntent = new Intent(mContext, AudioStreamingManager.class);
    stopIntent.setAction(Constants.ACTION.CLOSE_ACTION);
    PendingIntent closeIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 3, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);


    Intent nI = new Intent(mContext, AudioStreamingManager.class);
    nI.setAction(Constants.ACTION.NEXT_ACTION);
    PendingIntent nextIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 4, nI, PendingIntent.FLAG_CANCEL_CURRENT);

    //Intent for Pause
    Intent prI = new Intent(mContext, AudioStreamingManager.class);
    prI.setAction(Constants.ACTION.PREV_ACTION);
    PendingIntent previousIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 5, prI, PendingIntent.FLAG_CANCEL_CURRENT);


    Intent lI = new Intent(mContext, AudioStreamingManager.class);
    lI.setAction(Constants.ACTION.DISLIKE_ACTION);
    PendingIntent likeIntent = PendingIntent.getService(mContext, (int) System.currentTimeMillis() + 1, lI, PendingIntent.FLAG_CANCEL_CURRENT);





    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);

    android.app.NotificationManager mNotificationManager = (android.app.NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, reminderName, NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(message);
        mContext.getSystemService(NotificationManager.class).createNotificationChannel(channel);

        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }

    }


    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, channelId)
            .setOngoing(true)
            .setAutoCancel(false)
            .setSmallIcon(R.drawable.ic_notification)
            .addAction(R.drawable.baseline_thumb_down_black_24, "Dislike", dislikeIntent)
            .addAction(R.drawable.ic_rew_dark, "Previous", previousIntent)
            .addAction(R.drawable.ic_pause, "Pause", pauseIntent)
            .addAction(R.drawable.ic_fwd_dark, "Next", nextIntent)
            .addAction(R.drawable.baseline_thumb_down_black_24, "Like", likeIntent)
            .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                    .setShowActionsInCompactView(1, 2, 3)
                    .setMediaSession(mediaSession.getSessionToken()))
            .setContentText(message)
            .setContentTitle(title)
            .setColor(mContext.getResources().getColor(R.color.colorAccent))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true);

    if (bitmap.length > 0) {
        builder.setLargeIcon(bitmap[0]);
    }

    if (actions != null) {
        for (NotificationCompat.Action action : actions) {
            builder.addAction(action);
        }
    }


    Notification notification = builder.build();
    NotificationManagerCompat.from(mContext).notify(notificationId, notification);
}

Мой получатель был объявлен в манифесте, и вот он:

 private class AudioStreamingManager extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        //This is the code that is not being called
    }
}

Отображается уведомление, но кнопки управления мультимедиа не работают. Пожалуйста, помогите мне понять, почему.

...