android - 当我的应用程序运行时,我无法收到推送通知,如果它已关闭,我会收到通知
问题描述
我正在使用 firebase 处理推送通知。当我的应用程序关闭或在后台时,我可以收到通知,但是当我的应用程序运行时我没有收到通知请帮助我,这样即使我的应用程序正在运行,我也可以收到通知
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
shownotifeaction(remoteMessage.getNotification().getBody());
}
public void shownotifeaction(String message) {
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, Splash_Img.class), 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Porwal")
.setContentText(message)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorAccent" />
解决方案
我建议您从您的服务器尝试这种形式的 json:
{
"data":
{
"title":"FCM Message", "Body":"This is an FCM Message" },
"to" : "devicetoken"
}
解释:
通知 - 仅当您的应用程序处于后台/被杀死时直接进入 Android 通知托盘的消息,或者如果您的应用程序处于前台,则传递到 onMessageReceived() 方法。
数据负载 - 无论您的应用程序是在前台还是后台还是被杀死,这些消息都将始终传递给 onMessageReceived() 方法。
推荐阅读
- python - 我需要在第 n 个索引处插入一行,该行将对其下方的所有行求和
- mysql - 在 MySQL 中转置具有不同数据类型的数据库
- css - 如何使图像高度包含文本div的一定百分比?
- kubernetes - 无法使用 hostAliases 更新 kubernetes 容器 /etc/hosts 文件
- node.js - EPERM:不允许操作,读取
- python - 如何知道我的脚本是在后台还是前台运行?
- r - R中XGBoost中混淆矩阵的缺失部分
- c++ - 具有字符串文字构造函数的类不适用于 const 引用初始化
- ios - 使用苹果登录:每次登录时,都会出现警报,并显示“需要双重身份验证”消息。无法登录
- c# - 为什么使用 ResourceDictionary 时不显示颜色/画笔?