首页 > 解决方案 > 当应用程序不在后台时没有得到大图标

问题描述

我正在使用以下代码为推送通知设置大图标。

 new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.logo)
                    .setContentTitle(notification.getTitle())
                    .setContentText(notification.getBody())
                    .setAutoCancel(true)
                    .setColor(getResources().getColor(R.color.green))
                    .setSound(defaultSoundUri)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo))
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentIntent(pendingIntent);

这可以正常工作,并在应用程序处于前台时显示largeIcon,但当应用程序不在前台时,它不会显示大图标。

我正在测试应用程序samsung s7(oreo)

标签: androidandroid-intentpush-notification

解决方案


您发布的代码片段仅在您的应用程序处于前台时才能正常工作,因为它仅在应用程序处于前台时才会被调用。当应用程序处于后台时,Android 系统会为您显示通知(基于服务器发送给设备的 JSON 对象中的“通知”对象)。您的接收器根本没有被呼叫。目前,Firebase 云消息传递不支持在请求中将大图标设置为有效负载。

解决此问题的一种方法是不在消息的 POST 有效负载中包含“通知”对象。当您仅在有效负载中包含“数据”对象时,即使您的应用程序在后台,您的接收器也会被要求处理通知。然后,您可以像应用程序在前台一样构建通知,并为通知设置一个大图标。

查看此答案以获取有关此问题的更详细说明。


推荐阅读