0

I am creating a media player application. I want to show a notification on notification bar, with play, pause, forward, backward and close functionality. Notification is successfully displayed on Notification bar. but I am not able to give any click event on this..

Notification panel

public class NotificationPanel {

    Context mContext;
    NotificationManager mManager;
    NotificationCompat.Builder builder;
    RemoteViews remoteViews;
    int NOTIFICATION_ID = 1234;
    ImageButton play_pause;

    public NotificationPanel(Context mContext) {
        this.mContext = mContext;
        builder = new NotificationCompat.Builder(mContext).
                setContentTitle("SONORE").setSmallIcon(R.mipmap.dark_logo2).setOngoing(true);

        remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification_bar_layout);

        setListeners(remoteViews);

        builder.setContent(remoteViews);

        mManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        mManager.notify(NOTIFICATION_ID, builder.build());

    }

    private void setListeners(RemoteViews remoteViews) {

        //listener to pause song
        Intent pause = new Intent(mContext, NotificationReturnSlot.class);
        pause.putExtra("DO", "pause_play");
        PendingIntent playPausebtn = PendingIntent.getActivity(mContext, 1, pause, 0);
        remoteViews.setOnClickPendingIntent(R.id.fab_play, playPausebtn);

        //listener to remove notification bar
        Intent remove_notification = new Intent(mContext, NotificationReturnSlot.class);
        remove_notification.putExtra("DO", "remove_notification");
        PendingIntent remove_notification_btn = PendingIntent.getActivity(mContext, 2, remove_notification, 0);
        remoteViews.setOnClickPendingIntent(R.id.remove_notification, remove_notification_btn);

        //listener to forward song
        Intent forward = new Intent(mContext, NotificationReturnSlot.class);
        forward.putExtra("DO", "forward");
        PendingIntent forwardbtn = PendingIntent.getActivity(mContext, 3, forward, 0);
        remoteViews.setOnClickPendingIntent(R.id.forward, forwardbtn);

        //listener to backward song
        Intent backward = new Intent(mContext, NotificationReturnSlot.class);
        forward.putExtra("DO", "backward");
        PendingIntent backbtn = PendingIntent.getActivity(mContext, 4, backward, 0);
        remoteViews.setOnClickPendingIntent(R.id.forward, backbtn);

    }

    public void notificationCancle() {
        mManager.cancel(NOTIFICATION_ID);
    }

    public void changeToPlayIcon() {


    }
}

From this activity i am getting click lisenter event, and start a broadcast Manager to call my method from my main activity.

NotificationReturnSlot.activity

public class NotificationReturnSlot extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        int position;

        String action = (String) getIntent().getExtras().get("DO");
        if (action.equals("pause_play")) {
            position = 1;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("remove_notification")) {
            position = 2;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("forward")) {
            position = 3;
            callMethodOfDrawerActivityFromNotificationBar(position);

        } else if (action.equals("backward")) {
            position = 4;
            callMethodOfDrawerActivityFromNotificationBar(position);

        }


        finish();

    }

    public void callMethodOfDrawerActivityFromNotificationBar(int pos) {
        Intent intent = new Intent("notification-event");
        // add data
        intent.putExtra("notification", pos);

        LocalBroadcastManager.getInstance(this).

                sendBroadcast(intent);
    }
}

from MainActivity i am getting the broadcast event by the following way

     @Override
        protected void onResume() {
            super.onResume();

            //Register Notification receiver
            LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("notification-event"));
        }


     private BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int pos = intent.getIntExtra("notification", 0);

                switch (pos) {

                    case 0:
                        //do nothing
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 1:
                        if (isSongPlaying()) {
                            pauseSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        } else {
                            playSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        }
                        break;
                    case 2:
                        mPanel.notificationCancle();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 3:
                        mService.playNextSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                        break;
                    case 4:
                        mService.playPreviousSong();
 LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mNotificationReceiver);
                }
            }
        };
Himanshu Shekher Jha
  • 1,334
  • 2
  • 14
  • 27
  • Possible duplicate: http://stackoverflow.com/questions/17926236/notificationlistenerservice-implementation – ajameswolf Sep 20 '15 at 17:48
  • actually when i give any click on my notification layout, it did not call given method – Himanshu Shekher Jha Sep 20 '15 at 17:54
  • @ajameswolf **This does not belong on Code Review.** Broken code is off-topic there, and questions containing it will be closed. **Please read the [help center there](http://codereview.stackexchange.com/help).** – Ethan Bierlein Sep 20 '15 at 17:59
  • 3
    @ajameswolf and please stop recommending Code Review willy-nilly if you don't really understand what's on-topic there – janos Sep 20 '15 at 18:00
  • @janos ok, what would you recommend in this case? it is too specific to be of any use? Similar to someone posting their coding homework on here. – ajameswolf Sep 20 '15 at 18:03

1 Answers1

0

I got my Answer. the problem is , i am calling remove broadcast from pause().. but it should be called after each action performed from Notification layout.

Himanshu Shekher Jha
  • 1,334
  • 2
  • 14
  • 27