Push Notification Icon Android

2019-02-27 20:47发布

问题:

How to add notification icon in notification area in android? I try this but this show a blank white space in notification area.

mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);        
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, Receive_Message_list.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this)           
        .setSmallIcon(R.drawable.pushicon)
        .setContentTitle("MAY-I")
        .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(notification_message))
                .setContentText(notification_message);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

回答1:

On android 5.0, the icon is passed thru a color filter that makes all non transparent pixels, white.

from, http://developer.android.com/design/patterns/notifications.html

Use color to distinguish your app from others. Notification icons should only be a white-on-transparent background image.



回答2:

private void showNotification(final String title, String text, int ID, boolean showTimeStamp) {

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) //Use a builder
                .setContentTitle(title) // Title
                .setContentText(text) // Message to display
                .setTicker(text).setSmallIcon(R.drawable.ic_notif_small) // This one is also displayed in ticker message
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bulb)); // In notification bar

        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        //mBuilder.addAction(R.drawable.bulb_small, "OK", resultPendingIntent);

        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS | Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

        long time = 0;
        if (showTimeStamp)
            Calendar.getInstance().getTimeInMillis();
        else
            time = android.os.Build.VERSION.SDK_INT >= 9 ? -Long.MAX_VALUE : Long.MAX_VALUE;

        notification.when = time;

        //notification.icon = R.drawable.ic_moneta_logo_small;

        //mNotificationManager.cancelAll(); //Clear all currently display notifications
        mNotificationManager.cancel(ID);
        mNotificationManager.notify(ID, notification);
    }