android - 活动不是从通知开始的
问题描述
我正在尝试将通知设置为直接指向特定活动。我按照官方文档中列出的步骤进行操作。但是单击通知只会打开应用程序的主启动器。
我试图通过通知启动的活动是 DetailActivity。
这就是我在 manifest.xml 中设置活动层次结构的方式
<activity
android:name=".SplashscreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeActivity"
android:parentActivityName=".SplashscreenActivity"/>
<activity
android:name=".DetailActivity"
android:parentActivityName=".HomeActivity"/>
在我onMessageReceived
的课堂方法中FirebaseMessagingService
,我有以下内容:
Intent resultIntent = new Intent(this, DetailActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent intent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager.notify(
NOTIFICATION_ID,
getNotification("title", "text", intent)
);
getNotification
方法:
private Notification getNotification(String title, String text, PendingIntent intent) {
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setContentIntent(intent)
.setOngoing(true)
.build();
}
我不知道这个问题是否是我尝试启动的活动不是启动器活动的直接子项。而且我也不知道如何调试它。希望有人以前遇到过这个奇怪的问题!
解决方案
用这个:
Intent notificationIntent = new Intent(this, DetailActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent. FLAG_ONE_SHOT);
并在通知中使用这个pendingIntent。
推荐阅读
- c - 如何防止闪存块故障?
- python - 如何绘制一个给定中心坐标、宽度、高度和角度的填充旋转矩形?
- tensorflow - 有没有办法使用 TensorFlow 的 Dataset API 对 tf.Dataset 进行分区?
- python - 下载 ResNet、VGG 等的预训练 ImageNet 模型(.PB 文件)
- swift - 将 Firebase 快照分配给变量
- c++ - 是否可以仅通过将指针作为参数传递来复制二叉搜索树?
- python - Pandas - 合并所有具有相同子字符串的列
- javascript - 使用 SVG Rect On Click 处理程序更改 React 状态
- python - 二进制多项式模 - Python
- javascript - Snap.svg 转换不起作用 - 元素选择可能存在问题?