java - 关闭应用程序时无法检索 FCM 通知
问题描述
我正在将来自 firebase 云消息传递的通知检索到我的 android 应用程序中。但问题是当我的应用程序打开或处于运行状态时,只有我的应用程序正在检索通知。如果我没有使用我的应用程序或我的应用程序未打开,并且我发送任何推送通知,通知未在我的应用程序内检索。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
mFirestore = FirebaseFirestore.getInstance();
if (remoteMessage.getData() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
sendNotificationAPI26(remoteMessage);
else
sendNotification(remoteMessage);
}
}
private void sendNotification(RemoteMessage remoteMessage) {
isNotificationMatching = false;
Intent intent = new Intent(this, Dashboard.class);
intent.putExtra("notificationFragment", "showNotifications");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
final Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String description = data.get("description");
String url = data.get("url");
String code = data.get("code");
String actual_price = data.get("actual_price");
String deal_price = data.get("deal_price");
String key = data.get("key");
final String id = data.get("id");
mFirestore.collection("notifications").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.getData().get("id").equals(id)) {
isNotificationMatching = true;
break;
}
}
if (!isNotificationMatching) {
postDataToFirebaseServer(data);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.app_logo)
.setContentText(title)
.setAutoCancel(true)
.setSound(defaultSoundUri);
builder.setContentIntent(pendingIntent);
NotificationManager noti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
noti.notify(new Random().nextInt(), builder.build());
}
private void postDataToFirebaseServer(Map<String, String> data) {
Map<String,Object> postDataMap = new HashMap<>();
postDataMap.put("title", data.get("title"));
postDataMap.put("description", data.get("description"));
postDataMap.put("url", data.get("url"));
postDataMap.put("id", data.get("id"));
postDataMap.put("code", data.get("code"));
postDataMap.put("actual_price", data.get("actual_price"));
postDataMap.put("deal_price", data.get("deal_price"));
postDataMap.put("key", data.get("key"));
postDataMap.put("timestamp", FieldValue.serverTimestamp());
mFirestore.collection("notifications").add(postDataMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
// Toast.makeText(getA.this, "Success", Toast.LENGTH_SHORT);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error = e.getMessage();
// Toast.makeText(Testing.this, "Failed", Toast.LENGTH_SHORT);
}
});
}
private void sendNotificationAPI26(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, Dashboard.class);
intent.putExtra("notificationFragment", "showNotifications");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
final Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String description = data.get("description");
String url = data.get("url");
String code = data.get("code");
String actual_price = data.get("actual_price");
String deal_price = data.get("deal_price");
String key = data.get("key");
final String id = data.get("id");
mFirestore.collection("notifications").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
if (document.getData().get("id").equals(id)) {
isNotificationMatching = true;
break;
}
}
if (!isNotificationMatching) {
postDataToFirebaseServer(data);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
NotificationHelper helper;
Notification.Builder builder;
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
helper = new NotificationHelper(this);
builder = helper.getLootBoxNotification(title, description, url, id, code, actual_price, deal_price, key, defaultSoundUri);
helper.getManager().notify(new Random().nextInt(), builder.build());
builder.setContentIntent(pendingIntent);
}
}
我希望当我的应用程序关闭或不处于运行状态时发送任何推送通知,该通知也将在应用程序内部检索。
解决方案
推荐阅读
- php - PHP 返回错误:BookinginfoController.php 第 101 行中的 ErrorException:遇到非数字值
- c# - 无法将类型 string[] 隐式转换为模型
- c# - C# 如何让其他变量与 char 数组配合,以便我可以使用 char 查找字母出现的次数
- cakephp - 来自不同数据源的具有相同表名的 CakePHP 3 烘焙模型
- html - 如何使用 $emit 在 angularjs 中与父控制器通信
- c# - 在 Sqlite 数据库 Unity 中保存为 4 位数字的系统日期
- php - 如何让我的界面只在一栏中发送多封邮件?
- javascript - Safari 的媒体记录 API 替代方案
- c++ - 为什么 lambda 在 clang 而不是 gcc 上崩溃?
- string - NodeMCU 数据类型 字符串大小