android - 当应用程序不在后台时没有得到大图标
问题描述
我正在使用以下代码为推送通知设置大图标。
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)
解决方案
您发布的代码片段仅在您的应用程序处于前台时才能正常工作,因为它仅在应用程序处于前台时才会被调用。当应用程序处于后台时,Android 系统会为您显示通知(基于服务器发送给设备的 JSON 对象中的“通知”对象)。您的接收器根本没有被呼叫。目前,Firebase 云消息传递不支持在请求中将大图标设置为有效负载。
解决此问题的一种方法是不在消息的 POST 有效负载中包含“通知”对象。当您仅在有效负载中包含“数据”对象时,即使您的应用程序在后台,您的接收器也会被要求处理通知。然后,您可以像应用程序在前台一样构建通知,并为通知设置一个大图标。
查看此答案以获取有关此问题的更详细说明。
推荐阅读
- c++ - Visual C++ LNK2028 和 LNK2019 错误
- javascript - Sql server 和浏览器时区计算
- java - Jvm:编译器需要在方法字节码的哪些位置指定堆栈映射帧?
- c++11 - std::function 作为委托构造函数中的参数
- store - 在以太坊区块链上存储用户数据
- types - 将多个变体组合成一个变体
- python - 如何从稀疏的非规范化表创建 xarray?
- ruby-on-rails - 使用 mina + unicorn + nginx 部署后未加载站点
- java - 如何在 Ksoap2 中将自定义 Array 对象设置为 soapObject.addProperty
- html - 自动返回链接的预悬停和预聚焦状态