我正在使用下面的通知负载,并使用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键值对在推送通知中包含了一个图像。我的预期输出是:

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

如您所见,图像没有出现。可能的问题是什么?
发布于 2021-03-20 18:21:43
我发现的问题出在积极节电的上。例如,小米和三星(特别是Android 11 )手机将限制FCM进行后台联网(以获取图像),因此,图像将丢失。只有当手机处于深度睡眠状态时,才会发生这种情况。如果不是这样(并且你的应用已经关闭或处于后台),图像就会显示出来。如果应用程序在前台,则会调用onMessageReceived(),您无论如何都需要手动获取图片,并且不会受到手机的限制。
遗憾的是,我没有找到任何解决方法,除了用户手动禁用你的应用程序的电池节省:/

发布于 2021-03-26 20:39:24
-short将图片上传到一个流行的图片托管网站,并将通知图片url的链接放在我身上。还要检查我使用的图像大小(1000×500像素)
扩展了我也面临着同样的问题,但是创建MyFirebaseMessagingService并没有什么帮助。我试着从自己的主机(如https://www.example.com/image/img.png)加载图像。虽然它是https和firebase控制台显示的图像,但在实际的设备上它没有显示。
对我来说,将图片上传到imggur.com或将其放入firbase存储中,并使用该链接进行通知,无需任何额外的消息服务代码。
发布于 2019-08-30 19:58:09
这段代码可能会有帮助
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);
                }
            }) ;
        }
    }
}https://stackoverflow.com/questions/57725879
复制相似问题