An SO User
An SO User

Reputation: 24998

Notification won't display?

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>

Upvotes: 0

Views: 64

Answers (1)

hoomi
hoomi

Reputation: 1912

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

Upvotes: 1

Related Questions