android - FCM 通知不显示在后台弹出通知
问题描述
我正在使用 firebase 云功能在我的应用程序中显示通知,而我面临的唯一问题是当应用程序关闭或后台通知不显示弹出时。应用程序中使用了抬头通知,我确实收到了通知,但通知没有像应用程序处于前台时那样弹出。我已将通道和通知的优先级设置为高,但它仍然不起作用。
我的服务等级:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d("msg", "onMessageReceived: " + remoteMessage.getData().get("message"));
Intent intent = new Intent(this, WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.app_logo)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true).setContentIntent(pendingIntent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(Notification.PRIORITY_MAX);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
显现
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.noderon.riscalert.WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.noderon.riscalert.MainActivity">
</activity>
<service
android:name=".NotificationService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
</application>
任何帮助,将不胜感激。感谢你
解决方案
由于您已经设置了优先级,因此您似乎没有为通知添加元数据,并且当应用程序在后台时,它不会获取任何数据,这就是为什么它只在前台显示弹出窗口,将这些元数据添加到您的代码:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
此外,您可以查看Google Cloud Messaging文档以了解更多详细信息
快乐编码
推荐阅读
- html - Bootstrap 4 隐藏和显示网格
- reporting-services - SSRS - 你能计算出 2 个不同的 Tabllixes 之间的差异吗?
- syntax - 从 ansible_facts 重新格式化 ip 而不使用过滤器
- checkbox - Aurelia 中的动态单选框/复选框解决方案
- ios - 尝试构建时,出现fabric plist 错误
- java - 将 Hibernate Validator 注解处理器与 Kotlin(和 Gradle)一起使用
- javascript - 如何根据二维数组绘制关卡?
- google-cloud-platform - 如何禁用谷歌翻译 API 不翻译常用词的专有名称
- mysql - 如何比较位于不同系统(BigQuery 和 MySQL)中的具有复合键的两个表?
- java - 如何为 enitymanager 编写单元测试?