java - Android 应用程序在收到推送通知后自动导航到不同的活动
问题描述
我用 Firebase 功能和 firebase 云消息构建了一个 android 聊天应用程序。但我意识到一些很奇怪的事情。在通知显示之前,应用程序将自动导航到主要活动。可能是什么问题呢?我根本不知道为什么会发生这种情况。我猜它来自我的 OnMessageReceived 但我不确定。我将在下面发布它,以便更容易跟踪错误。
我的 OnMessage 已收到
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
showNotification(remoteMessage.getData().get("name"), (remoteMessage.getData().get("click_action")), remoteMessage.getData().get("title"));
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
}
}
private void showNotification(String name, String click_action, String title) {
Intent intent;
switch (click_action) {
case "Download":
intent = new Intent(this, Download.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "Student_SystemsDevt":
intent = new Intent(this, Student_SystemsDevt.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
case "Chats":
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
default:
intent = new Intent(this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
break;
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(name)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle())
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
下面是结构;
"data" : {
"name" : "What's up",
"title" : "Upward Studios",
"click_action" : "chats"
}
解决方案
推荐阅读
- c# - . 使用带有 NET Core 3.1 依赖注入的工厂模式
- oracle - 什么 Oracle 字典表包含表权限信息?
- php - 将单个字符串转换为单个数组组
- elasticsearch - 来自 Elastic Search 7.6.2 的随机文档 - 弃用警告消息
- python - 如何根据用户输入从 SQL 表中删除项目?
- vue.js - 如何在 vuetify 2 中对卡片进行环绕水平对齐?
- terraform - 如何在terraform的嵌套列表中切片第一个元素
- c++ - c++中的归并排序
- apache-camel - 当我的数字超过 1 时,camel gcp pubsub 属性 maxMessagesPerPoll 会产生问题
- postman - 查询 Cosmos DB 的正确授权标头是什么?