firebase 图像未显示在推送通知中

oxiaedzo  于 2022-12-14  发布在  其他
关注(0)|答案(6)|浏览(219)

我正在使用下面的通知负载,并使用Postman向Android设备发送推送通知:

{
    "to" : "/topics/xxxx" ,
    "data" : {
        "url" : "https://res.cloudinary.com/demo/image/upload/w_200/lady.jpg",
        "dl" : ""
    },
    "notification" : {
        "title" : "This is a sample notification from general2",
        "body" : "Rich notification testing (body)",
        "image": "https://res.cloudinary.com/demo/image/upload/w_200/lady.jpg"
    }

}

我已经使用image键值对在推送通知中包含了一个图像。

但这是它在手机上显示的内容:

正如您所看到的,图像没有显示。可能是什么问题?

ki1q1bka

ki1q1bka1#

我发现问题出在激进的节电器上。例如小米和三星(特别是Android 11)手机将限制FCM进行后台联网(以获取图像),因此,图像将丢失。这只会发生在手机处于深度睡眠状态时。如果不是(并且您的应用程序已关闭或处于后台),图像将显示。如果应用程序处于前台,将调用onMessageReceived(),无论如何您都需要手动获取图像,它不会受到手机的限制。
遗憾的是,我没有找到任何解决办法,除了用户手动禁用您的应用的电池节省:/

fruv7luv

fruv7luv2#

简而言之上传图像到一个流行的图像托管网站,并把该链接通知图像网址为我工作。还检查图像大小我使用(1000 × 500像素)
扩展我也面临同样的问题,但创建MyFirebaseMessagingService没有帮助。我试图从自己的主机加载图像,如(https://www.example.com/image/img.png)。虽然它是https和firebase控制台显示图像,但在实际设备上它没有显示。

对我来说,上传图像到imggur.com或把它放在firbase存储器中,并使用该链接进行通知,无需任何额外的消息服务代码。

hts6caw3

hts6caw33#

此代码可能会有所帮助

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMessagingServ";

    Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            sendNotification(bitmap);
        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        if(remoteMessage.getData()!=null)
            getImage(remoteMessage);
    }

    private void sendNotification(Bitmap bitmap){

        NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
        style.bigPicture(bitmap);

        Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,0);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "101";

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_MAX);

            //Configure Notification Channel
            notificationChannel.setDescription("Game Notifications");
            notificationChannel.enableLights(true);
            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
            notificationChannel.enableVibration(true);

            notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setContentTitle(Config.title)
                .setAutoCancel(true)
                .setSound(defaultSound)
                .setContentText(Config.content)
                .setContentIntent(pendingIntent)
                .setStyle(style)
                .setLargeIcon(bitmap)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_MAX);

        notificationManager.notify(1, notificationBuilder.build());

    }

    private void getImage(final RemoteMessage remoteMessage) {

        Map<String, String> data = remoteMessage.getData();
        Config.title = data.get("title");
        Config.content = data.get("content");
        Config.imageUrl = data.get("imageUrl");
        Config.gameUrl = data.get("gameUrl");
        //Create thread to fetch image from notification
        if(remoteMessage.getData()!=null){

            Handler uiHandler = new Handler(Looper.getMainLooper());
            uiHandler.post(new Runnable() {
                @Override
                public void run() {
                    // Get image from data Notification
                    Picasso.get()
                            .load(Config.imageUrl)
                            .into(target);
                }
            }) ;
        }
    }
}
sd2nnvve

sd2nnvve4#

你必须自己处理图像的接收,就像官方文档的最后一行说的:如图所示设置了通知后,此发送请求使接收客户端能够处理在有效负载中传递的图像。Firesebase official documentation

zxlwwiss

zxlwwiss5#

2021年工作溶液:

override fun onMessageReceived(remoteMessage: RemoteMessage)
    remoteMessage.notification?.let {
        sendNotification(it.title!!, it.body!!, it.imageUrl)  //image url in uri type
    }
}

private fun sendNotification(title: String, message: String, imageUri: Uri?) {        
    ...

    val imageBitmap = imageUri?.let {
        Glide.with(requireContext()).asBitmap().load(it).submit().get()

        //you pick one, Glide or Picasso

        Picasso.get().load(it).get()
    }

    ...
}
yvgpqqbh

yvgpqqbh6#

检查你的图像大小,主要是如果你喜欢使用小于或等于1 MB的图像,主要是你的问题会得到解决,我试过了,在小米设备中工作。

相关问题