新的NotificationCompat.BigPictureStyle()显示错误无法通知视图

时间:2018-01-19 07:11:45

标签: java android firebase bitmap push-notification

我想在通知栏中显示我正在使用此代码的大尺寸图片

我的图像来自server.so里面的AsyncTask转换为位图并在活动图像视图中显示它的工作正常但是当使用通知生成器加载通知中的相同位图时显示错误

 Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
                imgView.setImageBitmap(img);
  NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
                                .bigPicture(img))
                        .setAutoCancel(true);
   NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
   01-19 12:28:32.556 1737-1737/? W/ProgressBarDelegate: Unknown Drawable subclass, src=android.graphics.drawable.ScaleDrawable@3c75db5
        01-19 12:28:32.590 1737-1737/? E/StatusBar: couldn't inflate view for notification com.synergy.fcmdemo/0x0
   android.widget.RemoteViews$ActionException: view: android.widget.FrameLayout doesn't have method: setEmphasizedMode(boolean)
   android.widget.RemoteViews.getMethod(RemoteViews.java:851)
   android.widget.RemoteViews.-wrap5(RemoteViews.java)
   android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1411)
   android.widget.RemoteViews.performApply(RemoteViews.java:3425)
   android.widget.RemoteViews.apply(RemoteViews.java:3160)
   com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:615)
   com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1036)
   at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:1015)
   at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:2088)
   at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:472)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:163)
   at android.app.ActivityThread.main(ActivityThread.java:6205)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
   01-19 12:28:32.591 1737-1737/? E/StatusBar: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.synergy.fcmdemo user=UserHandle{0} id=0 tag=null key=0|com.synergy.fcmdemo|0|null|10137: Notification(pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE))
   01-19 12:28:32.594 1737-1737/? D/StatusBar: onNotificationRemoved:  Key: 0|com.synergy.fcmdemo|0|null|10137

2 个答案:

答案 0 :(得分:0)

您从api响应中获取图像并转换为Bitmap,但在您的通知构建器中添加了drawable图像,只需更改此

Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);              
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(img)
                    .setContentTitle("hahahha").setStyle(new NotificationCompat.BigPictureStyle()
                            .bigPicture(img))
                    .setAutoCancel(true); 

现在要大局,你必须设置风格

Notification notif = new Notification.Builder(context)
     .setContentTitle("hahahha")
     .setContentText("content")
     .setSmallIcon(R.drawable.ic_launcher)
     .setLargeIcon(bitmap)
     .setStyle(new Notification.BigPictureStyle()
     .bigPicture(img)
     .setBigContentTitle("hahahha"))
     .build();

答案 1 :(得分:0)

试试他,为我工作

使用主题从服务器获取图片

new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    showBigNotification();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

发送通知

private void showBigNotification() {
        Bitmap bitmap = getBitmapFromURL(**Your image url**);

        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.setBigContentTitle("title");
        bigPictureStyle.setSummaryText("message");
        bigPictureStyle.bigPicture(bitmap);

        Intent notificationIntent = new Intent(mContext, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext,
                101, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationManager nm = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);

        Resources res = mContext.getResources();
        Notification.Builder builder = new Notification.Builder(mContext);

        builder.setContentIntent(contentIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                .setTicker(res.getString(R.string.app_name))
                .setStyle(bigPictureStyle)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Title")
                .setContentText("Message");
        Notification n = builder.build();

        if (nm != null) {
            nm.notify(1001, n);
        }
    }

从服务器获取图片

public Bitmap getBitmapFromURL(String strURL) {
        try {
            URL url = new URL(strURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }