6

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

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

2 Answers2

19

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thank you..i have one more issue is that when my media player is playing music and if you start another player like spotify or playmusic app than my app should stop playing music..so how could achieve this...any guidance – Swap-IOS-Android Oct 26 '12 at 09:48
  • @SwapAndroid: Look into the audio focus APIs on `AudioManager`. – CommonsWare Oct 26 '12 at 14:08
  • @CommonsWare gives me a suggestion. I build a notification big view using RemoteView to control play/pause like this link (stackoverflow.com/questions/14508369/…) All are right but when i click device back button and out from the application click event(Play/Pause/Forward/Close) button doesn't work.Please help me. – Helal Khan Mar 11 '14 at 14:06
0

notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

Just Variable
  • 892
  • 10
  • 19
  • see : https://stackoverflow.com/a/8479088/4628115. Suggested edit `notification.flags |= ` – CybeX Jan 12 '18 at 20:52