待定意图会打开错误的活动

编程入门 行业动态 更新时间:2024-10-12 03:25:53
本文介绍了待定意图会打开错误的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用FirebaseMessagingService获取通知,并在单击通知后打开应用程序.但是,每次我单击通知时,应用程序都会打开MainActivity而不是预期的ResultActivity.我还关注了PendingIntent文档中的文档,并且仍然执行相同的操作.

I am using the FirebaseMessagingService for getting notifications and opening the app upon clickng the notification. But everytime i click the notification the app opens the MainActivity instead of the intended ResultActivity. I also followed docs from the PendingIntent docs and still does the same.

private void createNotification( String messageBody) { Intent intent = new Intent( this , ResultActivity.class ); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); // PendingIntent resultPending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this) .setContentTitle("VERA") .setContentText(messageBody) .setAutoCancel( true ) .setSound(notificationSoundURI) .setContentIntent(resultIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, mNotificationBuilder.build()); }

这是我的清单.

<activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".ResultActivity" android:launchMode="singleTask" android:excludeFromRecents="true" android:taskAffinity=""></activity>

我试图传递一些额外的字符串,但是主要活动甚至什么也没收到. notif是否可能仅触发其默认方法来启动该应用程序?

I tried to pass some extra strings, but the main activity is not even receiving anything. Is it possible that the notif is only triggering its default method to launch the app?

推荐答案

创建启动活动的Intent.

Create an Intent that starts the Activity.

通过使用标志FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK调用setFlags(),将活动"设置为开始一个新的空任务.

Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

通过调用getActivity()创建PendingIntent.

就像

Intent notifyIntent = new Intent(this, ResultActivity.class); // Set the Activity to start in a new, empty task notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Create the PendingIntent PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

然后,您可以像往常一样将PendingIntent传递给通知:

Then you can pass the PendingIntent to the notification as usual:

NotificationCompat.Builder mNotificationBuilder= new NotificationCompat.Builder(this, "CHANNEL_ID"); builder.setContentIntent(notifyPendingIntent); ... NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, mNotificationBuilder.build());

更多推荐

待定意图会打开错误的活动

本文发布于:2023-11-27 11:26:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意图   待定   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!