我正在使用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());更多推荐
待定意图会打开错误的活动
发布评论