首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >推送通知中未显示图片

推送通知中未显示图片
EN

Stack Overflow用户
提问于 2019-08-30 19:26:36
回答 5查看 6.8K关注 0票数 1

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

代码语言:javascript
运行
复制
{
    "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键值对在推送通知中包含了一个图像。我的预期输出是:

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

如您所见,图像没有出现。可能的问题是什么?

EN

回答 5

Stack Overflow用户

发布于 2021-03-20 18:21:43

我发现的问题出在积极节电的上。例如,小米和三星(特别是Android 11 )手机将限制FCM进行后台联网(以获取图像),因此,图像将丢失。只有当手机处于深度睡眠状态时,才会发生这种情况。如果不是这样(并且你的应用已经关闭或处于后台),图像就会显示出来。如果应用程序在前台,则会调用onMessageReceived(),您无论如何都需要手动获取图片,并且不会受到手机的限制。

遗憾的是,我没有找到任何解决方法,除了用户手动禁用你的应用程序的电池节省:/

票数 4
EN

Stack Overflow用户

发布于 2021-03-26 20:39:24

-short将图片上传到一个流行的图片托管网站,并将通知图片url的链接放在我身上。还要检查我使用的图像大小(1000×500像素)

扩展了我也面临着同样的问题,但是创建MyFirebaseMessagingService并没有什么帮助。我试着从自己的主机(如https://www.example.com/image/img.png)加载图像。虽然它是https和firebase控制台显示的图像,但在实际的设备上它没有显示。

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

票数 1
EN

Stack Overflow用户

发布于 2019-08-30 19:58:09

这段代码可能会有帮助

代码语言:javascript
运行
复制
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);
                }
            }) ;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57725879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档