推送通知无法与NotificationCompat.BigPictureStyle配合使用(无法为通知增加视图)

时间:2018-07-22 16:41:08

标签: android push-notification android-notifications

我正在尝试在通知托盘中显示大图图像。这是代码

Intent resultIntent = new Intent(mCtx, PostsActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        resultIntent.putExtra("notificationId", data.get("id"));
        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent, PendingIntent.FLAG_ONE_SHOT);
 Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Bitmap bitmap = getBitmapfromUrl
                (data.get("image"));
        String title = data.get("title");
        String body = data.get("body");
        //Bitmap picture = BitmapFactory.decodeResource(bitmap);
        final NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(mCtx, Constants.CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(title)
                        .setContentText(body)
                        .setAutoCancel(true)
                        .setSound(soundUri)
                        .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                        .setContentIntent(pendingIntent);

                NotificationManager mNotifyMgr =
                        (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);

                if (mNotifyMgr != null) {
                    mNotifyMgr.notify(0, mBuilder.build());
                } 


 public Bitmap getBitmapfromUrl(String imageUrl) {
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            return BitmapFactory.decodeStream(input);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
  

AndroidMenifest.xml

<service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
  

为BigPictureStyle应用代码时出现Logcat错误。

    07-28 23:02:38.157 1728-1728/? E/StatusBar: couldn't inflate view for notification com.notification.app/0x0
    android.widget.RemoteViews$ActionException: view: android.widget.FrameLayout doesn't have method: setEmphasizedMode(boolean)
        at android.widget.RemoteViews.getMethod(RemoteViews.java:851)
        at android.widget.RemoteViews.-wrap5(RemoteViews.java)
        at android.widget.RemoteViews$ReflectionAction.apply(RemoteViews.java:1411)
        at android.widget.RemoteViews.performApply(RemoteViews.java:3425)
        at android.widget.RemoteViews.apply(RemoteViews.java:3160)
        at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:603)
        at com.android.systemui.statusbar.BaseStatusBar.addNotificationViews(BaseStatusBar.java:972)
        at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:1940)
        at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:442)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6184)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:788)
07-28 23:02:38.158 1728-1728/? E/StatusBar: Couldn't expand RemoteViews for: StatusBarNotification(pkg=com.notification.app user=UserHandle{0} id=0 tag=null key=0|com.notification.app|0|null|10464: Notification(pri=0 contentView=null vibrate=null sound=content://settings/system/notification_sound defaults=0x0 flags=0x10 color=0x00000000 actions=1 vis=PRIVATE))

我可以在没有NotificationCompat.BigPictureStyle()的情况下接收通知。但是,当我使用新的 NotificationCompat.BigPictureStyle()时,我不会收到通知。我在整个互联网上搜索,但没有任何帮助。我不知道怎么了。

请帮助!

0 个答案:

没有答案