Swap-IOS-Android
Swap-IOS-Android

Reputation: 4383

what to code for notification to stick until music stop playing

I want notification which will stick on notification bar until music stop playing it. currently i have written some code in which i can show notification but when i press clear notification button or swap it than it disappear from notification center. I want notification like spotify which stays on bar until you stop playing music. Here is my code for notification

int pendingRequestCode = 0;
 // final Resources res = getResources();
    notificationManager = (NotificationManager) getSystemService(
           NOTIFICATION_SERVICE);
   Intent i = new Intent(getApplicationContext(),Mainactivity.class);   
   Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("test ckick")    
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), NOTIFICATION_DEFAULT, i,0));


       // Sets a custom content view for the notification, including an image button.
        layout = new RemoteViews(getPackageName(), R.layout.notification);
       layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
       Intent clickIntent = new Intent();
       clickIntent.setAction(ACTION_DIALOG);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
       layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
       builder.setContent(layout);

       // Notifications in Android 3.0 now have a standard mechanism for displaying large
       // bitmaps such as contact avatars. Here, we load an example image and resize it to the
       // appropriate size for large bitmaps in notifications.

       layout.setImageViewResource(R.id.notification_button, R.drawable.pause);
   notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());

waiting for reply enter image description here

Upvotes: 6

Views: 6796

Answers (2)

Just Variable
Just Variable

Reputation: 877

notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007554

Use setOngoing(true) to indicate that the event is ongoing. You may also wish to remove setAutoCancel(true), as that clears the Notification when the user taps upon it.

Upvotes: 19

Related Questions