androidandroid-notificationsremoteviewandroid-remoteview

Custom Notification is not displaying in Android


Hello I am display custom notification in my android application. But issue is that custom notification is not displaying. I saw many threads but did not find any answer related to my issue.

I could not find where is the issue. Can you please look at this.

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// display the notification
NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.notificationIcon, Util.notificationBitmap);
contentView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
contentView.setTextViewText(R.id.notificationContent, notificationMessage);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_stat_logo)
                    .setContentTitle(Util.notificationTitle)
                    .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(notificationMessage))
                    .setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .setContentText(notificationMessage);

mBuilder.setContent(contentView);
// build notification
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify((int)SystemClock.currentThreadTimeMillis(), mBuilder.build());

custom_notification.xml

<?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="match_parent" >

    <ImageView
        android:id="@+id/notificationIcon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />

    <TextView
        android:id="@+id/notificationTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/notificationIcon"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/notificationContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/notificationTitle"
        android:layout_toRightOf="@id/notificationIcon"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

Thanks in advance.


Solution

  • @TGMCians thanks :)

    https://chat.stackoverflow.com/transcript/15?m=16698842#16698842

    Probably using Support Library, remote views concept does not work in Android 2.X if you are looking for custom notification.

    This worked for me and solved my issue.

    // display the notification
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    
    // Android 2.x does not support remote view + custom notification concept using 
    // support library
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        mBuilder.setSmallIcon(R.drawable.ic_stat_logo);
        mBuilder.setContentTitle(Util.notificationTitle)
                .setStyle(
                        new NotificationCompat.BigTextStyle()
                                .bigText(notificationMessage))
                .setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND)
                .setLights(Color.WHITE, 500, 500)
                .setContentText(notificationMessage);
    } else {
        RemoteViews customNotificationView = new RemoteViews(getPackageName(),
                R.layout.custom_notification);
    
        customNotificationView.setImageViewBitmap(R.id.notificationIcon, Util.notificationBitmap);
        customNotificationView.setTextViewText(R.id.notificationTitle, Util.notificationTitle);
        customNotificationView.setTextViewText(R.id.notificationContent, notificationMessage);
    
        mBuilder.setContent(customNotificationView);
        mBuilder.setSmallIcon(R.drawable.ic_launcher);
        mBuilder.setAutoCancel(true);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setLights(Color.WHITE, 500, 500);
    }
    // build notification
    mBuilder.setContentIntent(pendingIntent);
    mNotificationManager.notify(1000, mBuilder.build());