0

so far I have been able to create custom notification when my player starts to play, but the problem is whenever i click the notification button that not only stops the media player button changes the button to stop icon.

But the thing is to achieve this i have to bring back the entire application from the dead and its kind of annoying when user click stop button in notification the entire application just resurfaces.

I am using intent to make a call on MainActivty that stops the player as well as updates the notification bar that the player has stopped.

There seems to be only normal examples of notification like displaying simple info and dismissing the notification on click of button. Pulling my hair for 5 days can some one help me with this! PLEASE ?

PS: i don't want to use the MediaStyle notification.

1 Answers1

1

You can use Activity without UI.

public class NotificationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //do your stuff
    finish();//end activity
}

public static PendingIntent getViewIntent(int id){
    Intent intent = new Intent(AppContext.context, NotificationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);      
    return PendingIntent.getActivity(AppContext.context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 }
}

In your manifest :

<activity
        android:name=".NotificationActivity"
        android:taskAffinity=""
        android:excludeFromRecents="true"
        android:theme="@android:style/Theme.NoDisplay">
    </activity>

And inside NotificationBuilder

 NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(AppContext.context);

 PendingIntent resultPendingIntent = NotificationActivity.getViewIntent(id);
    mBuilder.setContentIntent(resultPendingIntent);

To update your notification with new icons you have to recreate custom notification with your desired icons and show notification again using same notification id.

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

    int notificationID =100;// this ID has to be same as your previuos ID
    // mId allows you to update the notification later on.
    mNotificationManager.notify(notificationID, mBuilder.build());
Vygintas B
  • 1,624
  • 13
  • 31
  • So you are saying that I should create a new separate activity for notification apart from my MainActivity ? and `android:taskAffinity=""` why is this empty ? –  Jan 25 '17 at 12:36
  • The affinity indicates which task an activity prefers to belong to. So setting "" refferes to nothing. – Vygintas B Jan 25 '17 at 12:39
  • Sorry forgot to inform. you need to set resultPendingIntent on your stop/play button. So clicking it will open notification activity. For now clicking whole notification would open notification activity – Vygintas B Jan 25 '17 at 12:40
  • there was a kind of flickering issue in this, but somehow its gone after the second build and gradle resync. EDIT nope it just resurfaced! –  Jan 25 '17 at 13:17
  • Keep me posted if you have any issues or questions :) – Vygintas B Jan 25 '17 at 13:18
  • There is this flickering effect! Have you tried this solution your self ? –  Jan 25 '17 at 13:19
  • I guess its because here the activity is getting created and on finish its rapidly closing, i think your solution is kind of a hack –  Jan 25 '17 at 13:21
  • @AceFire could you try adding this `Intent.FLAG_ACTIVITY_NO_ANIMATION` inside getPendingIntent() method – Vygintas B Jan 25 '17 at 13:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133989/discussion-between-acefire-and-vygintas-b). –  Jan 25 '17 at 13:37