GCM通知图标重叠(两次相同的图标,小而大)

时间:2015-06-23 09:13:36

标签: android google-cloud-messaging android-notifications android-notification-bar

我在我的应用程序中使用GCM,在Android 4.1和4.4设备中通知图标似乎没问题,但在Android 5.1中,我可以看到两个非常小的图标和一个正常尺寸的图标。小的是在右下角与巨大的图标重叠。你可以在这张图片中看到它:

enter image description here

这是我的代码:

private void generateNotification(String message) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(Constants.COMING_FROM_NOTIFICATION, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        String appName = getResources().getString(R.string.app_name);       

        int width;
        int height;
        if (Util.checkAndroidVersionUpperOrEqualThan(11)){      
            width = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width); 
            height = getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
        }else{
            width = 64;
            height = 64;
        }

        image = Bitmap.createScaledBitmap(image, width, height, false);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setLargeIcon(image)
        .setContentTitle(appName)
        .setContentText(message)
        .setSound(defaultSoundUri)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .setCategory(NotificationCompat.CATEGORY_MESSAGE)
        .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0,notificationBuilder.build());
    }

1 个答案:

答案 0 :(得分:0)

Notification class关于setSmallIcon()的Android文档说明:

设置小图标资源,用于表示状态栏中的通知。扩展视图的平台模板将在左侧绘制此图标,除非还指定了大图标,在这种情况下,小图标将移动到右侧。

这意味着API会以某种方式忽略大图标。尝试删除检查版本的代码。不知道为什么会导致这种情况。