Facebook图像为大图标通知在不同的手机上显示为白色?

use*_*869 8 notifications android bitmap android-5.0-lollipop notification-icons

我在正确创建和发送通知时遇到此问题.我在Lollipop 5.0 update这里阅读了新的通知设计规范:https://material.google.com/style/icons.htmlhttps://material.google.com/patterns/notifications.html,我意识到图标需要white按顺序排列正确显示.这很好,我完全明白,但我希望我的通知显示一张图片,显然不能是白色的.然而,令我感到困惑的是,当我尝试在手机上呈现通知时,一切正常.它看起来像这样:

在此输入图像描述

但是,在我朋友的手机上,它看起来像一个白色方块,如下所示:

在此输入图像描述

这很奇怪,因为我们都有相同版本的Android,版本6.0.1.这是我用来发送通知的代码:

private class NotificationPicture extends AsyncTask<String, Void, Bitmap> {

        Context context;
        String userId;
        String postId;
        String name;
        String notificationBody;
        String from;

        public NotificationPicture(Context context) {
            super();
            this.context = context;
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            userId = params[0];
            postId = params[1];
            name = params[2];
            notificationBody = params[3];
            from = params[4];
            try {
                URL url = new URL("https://graph.facebook.com/" + userId + "/picture?type=large");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream in = connection.getInputStream();
                Bitmap  bitmap = BitmapFactory.decodeStream(in);
                Bitmap output;
                Rect srcRect;
                if (bitmap.getWidth() > bitmap.getHeight()) {
                    output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                    srcRect = new Rect((bitmap.getWidth()-bitmap.getHeight())/2, 0, bitmap.getWidth()+(bitmap.getWidth()-bitmap.getHeight())/2, bitmap.getHeight());
                } else {
                    output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
                    srcRect = new Rect(0, (bitmap.getHeight()-bitmap.getWidth())/2, bitmap.getWidth(), bitmap.getHeight()+(bitmap.getHeight()-bitmap.getWidth())/2);
                }

                Canvas canvas = new Canvas(output);
                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

                float r;

                if (bitmap.getWidth() > bitmap.getHeight()) {
                    r = bitmap.getHeight() / 2;
                } else {
                    r = bitmap.getWidth() / 2;
                }

                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawCircle(r, r, r, paint);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                canvas.drawBitmap(bitmap, srcRect, rect, paint);
                return output;
            } catch (IOException e) {
                FirebaseCrash.report(e);
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            super.onPostExecute(result);
            try {
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(notificationBody)
                        .setTicker(from + " has responded!")
                        .setAutoCancel(true)
                        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(notificationBody))
                        .setSmallIcon(R.drawable.ic_tabs_notification_2)
                        .setLargeIcon(result);
                Intent resultIntent = new Intent(context, CommentsActivity.class);
                setupPostDetails(notificationBuilder, resultIntent, postId, userId, name, context);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

build.gradle配置:

compileSdkVersion 23
buildToolsVersion "23.0.1"
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
defaultConfig {
    applicationId 'com.schan.tabs'
    multiDexEnabled true
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 16
    versionName "1.16"
    signingConfig signingConfigs.Tabs
}
Run Code Online (Sandbox Code Playgroud)

如果有人知道为什么会这样,那么一些见解会非常受欢迎.我有三星SM-G900V(三星S5)和我的朋友有一个One plus One手机,如果这有帮助.

小智 2

我认为您需要为 Android 6 及更高版本提供具有透明背景的新通知图标,因此当您在通知上设置图标时,如下所示:

.setSmallIcon(android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP ? R.drawable.ic_notif_with_transparent_bg : R.drawable.ic_notif_current)
Run Code Online (Sandbox Code Playgroud)

如果你想显示照片,也许你可以尝试使用RemoteViews