android - 点击 android 推送通知有时不会重定向到应用程序
问题描述
在点击推送通知时,用户通常会被重定向到应用程序,但有些用户在点击推送通知时不会被重定向到应用程序。
对他们来说,通知只是被忽略并且应用程序没有启动。
可能是这些用户很长时间没有打开应用程序,但这会影响重定向到应用程序的行为吗?我还尝试通过强制设备进入打盹模式来重现这一点,但我仍然被重定向到应用程序。
有人可以帮我解决这个问题,用户有时不会在点击推送通知时被重定向到应用程序。
这是我的 AndroidManifest.xml
<service
android:name=".screens.fcm.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
这是我的 FirebaseMessagingService
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, HomeActivity.class);
String notificationType = (remoteMessage.getData().get("notification_type") != null) ?
messageBody.getData().get("notification_type") : "";
if ((notificationType.equalsIgnoreCase("main")))
intent.putExtra("type", "main");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteMessage.Notification notification = remoteMessage.getNotification();
String notificationTitle = notification != null ? notification.getTitle() : "";
String notificationMessage = notification != null ? notification.getBody() : "";
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, "")
.setSmallIcon(R.drawable.notification)
.setContentTitle(notificationTitle)
.setContentText(notificationMessage)
.setAutoCancel(true)
.setGroup(GROUP_KEY_HOME_NOTIFICATIONS)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("",
"Channel1",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
解决方案
推荐阅读
- python - 如何轻松创建python包,保持文件夹结构和文件?
- python - 如何在列表中找到奇数索引值的乘积
- android - 对象值显示使用 JSON 解析对象和具有相同名称的字符串
- php - 如何修复代码在单个数据库中使用 20 个 datalist HTML 标记在 datalist 中显示 1000 行但在 datalist 中仅显示前 512 行?
- java - 使用 Https 运行集成的 Angular 项目和 Spring Boot
- mysql - 更新查询返回操作数应包含 1 列
- scala - java.net.SocketTimeoutException:使用 spark 2.2 将数据帧加载到 teradata 时连接超时
- jquery - jQuery getJSON 到 axios
- asp.net-core - 是否可以在 Windows 10 中检索统一写入筛选器的状态?
- sharepoint - 将 SharePoint 列表项轮流分配给个人列表