• 5

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I have a player, where when user choose a song, player makes a notification with 3 buttons: previous, play, next. This buttons are needed to choose the songs in background mode. But when I click on this buttons nothing is happened. Here's the code of notification:

Intent intentPrev = new Intent(this, NotificationReceiver.class);
        PendingIntent pendingIntentPrev = PendingIntent.getActivity(this, 0, intentPrev, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentPlay = new Intent(this, NotificationReceiver.class);
        PendingIntent pendingIntentPlay = PendingIntent.getActivity(this, 0, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);

        Intent intentNext = new Intent(this, NotificationReceiver.class);
        PendingIntent pendingIntentNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.notification).
                addAction(R.drawable.previous, "Previous", pendingIntentPrev).addAction(R.drawable.pause, "Pause", pendingIntentPlay).
                addAction(R.drawable.next, "Next", pendingIntentNext);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);

And here's the code of Receiver:

package asus.example.com.player;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.Objects;

public class NotificationReceiver extends BroadcastReceiver {

    private final String    ACTION_PREV = "PREVIOUS";
    private final String    ACTION_PLAY = "PLAY";
    private final String    ACTION_NEXT = "NEXT";
    private final MyService service     = new MyService();

    public void onReceive(Context context, Intent intent) {
        if (Objects.equals(intent.getAction(), ACTION_PREV)){
        else if (Objects.equals(intent.getAction(), ACTION_NEXT)){

Also I added receiver to Manifest:

                <action android:name="PREVIOUS"/>
                <action android:name="PLAY"/>
                <action android:name="NEXT"/>

When I watched in debugger I saw that Receiver class isn't used, but I don't understand why

Right now, you are sending a broadcast immediately with statements like this:


And you say you want to start an Activity later on:

PendingIntent pendingIntentNext = PendingIntent.getActivity(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

(Side note: this won't work anyway because in intentNext, you specify a class extending from BroadcastReceiver not from Activity)

It should be getBroadcast() not getActivity():

Intent intentNext = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntentNext = PendingIntent.getBroadcast(this, 0, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);

Another thing: you are using '0' as request code (second parameter) in all your PendingIntents. You have to use different values for the request codes because two Intents sharing the same request code are considered to be the same Intent, so you'd end up with all three notification Buttons triggering the same action.

  • 1
Reply Report