3

i have a completely working notification with custom layout however i don't know how to update actions in specific situations

i'm using this notification to start and stop media player outside the app but i need some kind of updating the action (pending intent button)

for example: whenever i hit play it should add stop action (update notification with new action)

here's the code:

@Override
public void onCreate() {
    super.onCreate();

    showNotification();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {

        startPlaying();

    }else if (intent.getAction().equals(Constants.ACTION.STOP_ACTION)) {

        stopPlaying();
    }
    return START_STICKY;
}

private void showNotification() {

    // Using RemoteViews to bind custom layouts into Notification

    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.status_bar);
    RemoteViews bigViews = new RemoteViews(getPackageName(),
            R.layout.status_bar_expanded);


    views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
    views.setViewVisibility(R.id.status_bar_album_art, View.GONE);
    bigViews.setImageViewBitmap(R.id.status_bar_album_art,
            Constants.getDefaultAlbumArt(this));

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);

    Intent playIntent = new Intent(this, IZIGaapsCustomNotification.class);
    playIntent.setAction(Constants.ACTION.PLAY_ACTION);
    PendingIntent pplayIntent = PendingIntent.getService(this, 0,
            playIntent, 0);

    Intent nextIntent = new Intent(this, IZIGaapsCustomNotification.class);
    nextIntent.setAction(Constants.ACTION.STOP_ACTION);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);


    //Whenever i hit play, beneath buttons (actions) should appear

    views.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);


    //this should be appear next to above button(when above button clicked)

    views.setOnClickPendingIntent(R.id.status_bar_next, pstopIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, pstopIntent);


    views.setTextViewText(R.id.status_bar_track_name, "Song Title");
    bigViews.setTextViewText(R.id.status_bar_track_name, "Song Title");

    views.setTextViewText(R.id.status_bar_artist_name, "Artist Name");
    bigViews.setTextViewText(R.id.status_bar_artist_name, "Artist Name");


    status = new Notification.Builder(this).build();
    status.contentView = views;
    status.bigContentView = bigViews;
    status.flags = Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.ic_launcher;
    status.contentIntent = pendingIntent;
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, status);
}

}

any help will appreciated

pourya011
  • 91
  • 2
  • 13

1 Answers1

1

Here is an example of how to update a custom notification

private static final int NOTIF_ID = 1234;
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotificationManager;
private RemoteViews mRemoteViews;
private Notification mNotification;
private void setUpNotification(){

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // we need to build a basic notification first, then update it
    Intent intentNotif = new Intent(this, MainActivity.class);
    intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);
Intent playIntent = new Intent(this, IZIGaapsCustomNotification.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getService(this, 0,
        playIntent, 0);
    // notification's layout
    mRemoteViews = new RemoteViews(getPackageName(), R.layout.custom_notification_small);
    // notification's icon
    mRemoteViews.setImageViewResource(R.id.status_bar_play, R.drawable.play);
mRemoteViews.setOnClickPendingIntent(R.id.status_bar_play, pplayIntent);

    mBuilder = new NotificationCompat.Builder(this);

    CharSequence ticker = getResources().getString(R.string.ticker_text);
    int apiVersion = Build.VERSION.SDK_INT;

    if (apiVersion < VERSION_CODES.HONEYCOMB) {
        mNotification = new Notification(R.drawable.ic_launcher, ticker, System.currentTimeMillis());
        mNotification.contentView = mRemoteViews;
        mNotification.contentIntent = pendIntent;

        mNotification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
        mNotification.defaults |= Notification.DEFAULT_LIGHTS;

        // starting service with notification in foreground mode
        startForeground(NOTIF_ID, mNotification);

    }else if (apiVersion >= VERSION_CODES.HONEYCOMB) {
        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(false)
                .setOngoing(true)
                .setContentIntent(pendIntent)
                .setContent(mRemoteViews)
                .setTicker(ticker);

        // starting service with notification in foreground mode
        startForeground(NOTIF_ID, mBuilder.build());
    }
}

use this method to update the Notification's UI in your reciver for both play or pause send the action to this method

 private void updateNotification(String action ){

        int api = Build.VERSION.SDK_INT;
        //update action 
 Intent nextIntent = new Intent(this, IZIGaapsCustomNotification.class);
    nextIntent.setAction(action);
    PendingIntent pnextIntent = PendingIntent.getService(this, 0,
            nextIntent, 0);
        // update the icon
        mRemoteViews.setImageViewResource(R.id.status_bar_play, R.drawable.icon_off2);

mRemoteViews.setOnClickPendingIntent(R.id.status_bar_play, pnextIntent);
        // update the notification
        if (api < VERSION_CODES.HONEYCOMB) {
            mNotificationManager.notify(NOTIF_ID, mNotification);
        }else if (api >= VERSION_CODES.HONEYCOMB) {
            mNotificationManager.notify(NOTIF_ID, mBuilder.build());
        }
    }
tamtom
  • 2,474
  • 1
  • 20
  • 33
  • ok @tamtom i tested it out and it has deprecated methods on it any better solution?i'm targeting api 21 to above – pourya011 Dec 13 '17 at 09:36
  • what are the deprecated methods? – tamtom Dec 13 '17 at 09:37
  • @pourya011 you have to add channel id this is for Android Oreo `Builder(MainActivity.this, "channel_id")` check this answer for further reading https://stackoverflow.com/questions/45462666/notificationcompat-builder-deprecated-in-android-o and always if you saw any deprecated method search its name on google to see what is the replacement. please let me know if you need other help! – tamtom Dec 13 '17 at 09:44
  • thanks again for your time, of course, let me first check the link you post – pourya011 Dec 13 '17 at 09:48
  • @pourya011 if it helped please mark it as a correct answer. – tamtom Dec 13 '17 at 11:03