0

I have the following code to display a notification with custom view:

private void showMediaPlayerNotification(){
        RemoteViews rm = new RemoteViews(getActivity().getPackageName(), R.layout.notif_media_player);
        Intent activity = new Intent(getActivity(), HomeActivity.class);
        PendingIntent pendingActivity = PendingIntent.getActivity(getActivity(), 0, activity,0);

        Notification notif =  new NotificationCompat.Builder(getActivity())
                              .setContent(rm).setContentIntent(pendingActivity)
                              .setOngoing(true)
                              .build();
        NotificationManager mgr = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(NOTIFICATION_ID, notif);

    }  

This is inside a fragment. The notification will be shown when the fragment goes into onStop() and will be removed in onResume() and onDetach().

However, the notification isn't displayed.

What is wrong here?

This was run on a Samsung Galaxy S3.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="@android:color/black">

    <!-- Logo -->
    <ImageView android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:scaleType="fitXY"
        android:src="@drawable/chimp"
        android:layout_alignParentLeft="true" />

    <!-- Media Buttons -->
    <ImageButton android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_cancel"
        android:background="@android:color/transparent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_next"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/cancel"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_previous"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/next"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>

    <ImageButton android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_pause"
        android:background="@android:color/transparent"
        android:layout_toLeftOf="@id/previous"
        android:layout_marginRight="15dp" 
        android:layout_centerVertical="true"/>
</RelativeLayout>
An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

1

You also need to add setSmallIcon(int drawable). That should do the trick.

hoomi
  • 1,882
  • 2
  • 16
  • 17
  • Works like a charm ! Now, how do I respond to the buttons inside the remote view ? :) – An SO User Aug 24 '14 at 09:45
  • You will need to use `setOnClickPendingIntent` on you remoteViews – hoomi Aug 24 '14 at 09:48
  • great ! and can I access the `logo` ImageView from my RemoteView to change the image at run time ? – An SO User Aug 24 '14 at 09:57
  • I don't think you can change it like that. I think you need to update the whole notification with the logo imageview being updated. – hoomi Aug 24 '14 at 09:59
  • How does Google Play Music does it then ? :) I am certain they are using a custom view :) – An SO User Aug 24 '14 at 10:00
  • Also, the notification cancels when I open the app again **but** if I swipe the app off from recents, it stays. Weird. – An SO User Aug 24 '14 at 10:01
  • that is because you used `setOngoing(true)`. That prevents the app notification from being cleared – hoomi Aug 24 '14 at 10:08
  • So, how do I handle that? I mean, when the app is swiped out, it should go away. :) – An SO User Aug 24 '14 at 10:08
  • then use `setOngoing(false)` – hoomi Aug 24 '14 at 10:10
  • It is a media player, mate :) How do you see that working out? :) – An SO User Aug 24 '14 at 10:12
  • Do you mean swipe out from the recents? have you tried setAutoCancel()? And if it is a MediaPlayer why don't you set the notification from the service that you are using to play the music using `startForeground` and `stopForeground` – hoomi Aug 24 '14 at 10:20
  • Help me with this: http://stackoverflow.com/questions/25475142/pendingintent-to-communicate-from-notification-how-to – An SO User Aug 24 '14 at 18:51