关键词:
【中文标题】点击打开登录活动而不是主要活动的通知【英文标题】:Notification on click opening login activity instead of main activity 【发布时间】:2019-03-08 17:10:48 【问题描述】:我的应用中有基于 Firebase 的通知。从服务器收到消息后,我可以在手机中看到通知。但是当我点击通知中的消息时,它总是打开我的启动画面,然后是 LoginActivity 而不是 MainActivity。我想打开 MainActivity 并显示消息详细信息,为此我使用“putExtra”将通知的消息正文传递给 MainActivity。
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getTitle().toString(), remoteMessage.getNotification().getBody());
//This method is only generating push notification
private void sendNotification(String messageTitle, String messageBody)
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Title", messageTitle);
intent.putExtra("data", messageBody);
intent.putExtra("KEY", "Notify");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
numNotifications = numNotifications + 1;
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
inboxStyle.setBigContentTitle(getString(R.string.app_notification)) ;
inboxStyle.setSummaryText(getString(R.string.app_count_notifications1)
+ numNotifications + getString(R.string.app_count_notifications2));
inboxStyle.bigText(messageBody);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.q_me_notify)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(inboxStyle)
.setGroup(TAG)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try
notificationManager.notify(TAG, APPID, notificationBuilder.build());
catch (Exception e)
e.printStackTrace();
我的 Android 清单文件如下所示:
<application
android:allowBackup="true"
android:icon="@drawable/final"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="@style/LoginTheme"
android:launchMode="singleTask"/>
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"
android:launchMode="singleTask"/>
<!-- Defining Services -->
<service android:name=".services.FirebaseMessageService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.FirebaseInstanceService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
这发生在我的带有 Android 7.0 的 Samsung On8 (J7 Pro) 中 - 无论应用程序是在前台还是后台(它总是打开 LoginActivity)。但是在我的 Nexus 5 有 Android 5.1.1 时,只有当应用程序处于后台/终止时才会出现这种行为,而当应用程序处于前台时它可以正常工作。
所以,我的问题是:
无论型号和操作系统版本如何,如何让应用在前台显示 MainActivity,以便正确显示通知消息详细信息?
当应用程序处于后台/终止状态时,如何跨(Splash --> LoginActivity --> MainActivity)传递通知值,以便在 MainActivity 中正确显示消息?
【问题讨论】:
【参考方案1】:添加TaskStackBuilder
,替换:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
用这个:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
编辑:显然在您的MainActivity
中,您应该有逻辑从Intent
检索数据并更新Activity
【讨论】:
【参考方案2】:在浏览了几篇文章和博客后,我解决了这个问题。对我有很大帮助的最好的和详细的答案是 Mukesh Rana 在这篇文章中的回答 Clicking on notification doesn't open mentioned activity
我现在使用 REST api 从服务器发送“数据”有效负载,无论应用程序是在前台还是后台,它总是被拾取。如果应用程序处于后台,通知的 onClick 始终会启动启动画面(启动器)。在 onMessageReceived 中,我使用 remoteMessage.getData() 获取“数据”。在意图中,我使用 intent.putExtra(key, value) 将值传递给意图(后台应用程序默认为启动画面)。从那里我将它传递给 MainActivity 并使用 getStringExtra(key) 获取值。
【讨论】:
fcm点击应用处于后台状态时如何打开用户指定的活动而不是默认活动(代码片段)
...但我的挑战是当应用程序处于后台状态时通知到来时,当点击时,它会重定向到默认活动,但我想导航到指定的活动。这是MyFirebaseMessagingService类:publicclassMyFirebaseMessagingServiceextendsFirebaseMessagingServiceprivatestaticfinalStringTAG="MyFirebas... 查看详情
点击通知打开新活动
】点击通知打开新活动【英文标题】:clicknotificationtoopennewactivity【发布时间】:2018-05-2715:24:58【问题描述】:我的应用中有一条通知,其中包含以下代码:我的通知很好,但我的问题是我应该做什么,以便在单击我的通知后开... 查看详情
推送通知以打开特定活动
...它都会先启动(A),然后启动(B)。我想制作一个推送通知,点击后它可以访问(C)但必须先启动(B)。我正在使用 查看详情
java示例代码_安卓-点击通知打开我的主要活动,我如何让它打开其他内容
java示例代码_安卓-点击通知打开我的主要活动,我如何让它打开其他内容 查看详情
单击 fcm 通知而不是打开启动器活动时打开浏览器
】单击fcm通知而不是打开启动器活动时打开浏览器【英文标题】:Openbrowserwhenclickedonfcmnotificationinsteadofopeninglauncheractivity【发布时间】:2020-05-0504:00:27【问题描述】:我想将带有FCM的url发送到android应用程序。单击FCM通知而不是打... 查看详情
确定是不是从 FCM 通知调用活动
...数据。因此,只要应用程序在后台,主活动就会在通知上打开。我的主要活动有4个选项卡。如果应用程序是从通知中打开的,我想打开Tab4,否则是Tab2。我想检查主要活 查看详情
通知点击:活动已开启
】通知点击:活动已开启【英文标题】:Notificationclick:activityalreadyopen【发布时间】:2012-08-1603:18:50【问题描述】:我有一个带有通知的应用程序,如果我单击它们会打开某个活动。我想要这样,如果我点击通知并且活动已经打... 查看详情
通知打开活动,按下后退按钮,打开主要活动?
】通知打开活动,按下后退按钮,打开主要活动?【英文标题】:Notificationopensactivity,backbuttonpressed,mainactivityisopened?【发布时间】:2011-11-2816:33:16【问题描述】:我可以描述我的问题的最佳方式是这样的:在启动时会创建一个通... 查看详情
单击通知时打开错误的活动
...ingNotification【发布时间】:2019-09-0400:03:23【问题描述】:点击通知后,错误的Activity被打开-主应用程序Activity,但不是我的目标Activity-NearPhotoActivity。创建通知:publicstaticNotificationcreateNotification( 查看详情
从 firebase 通知中打开特定活动
...不仅仅是打开应用程序。就像一个通知,它会推动用户在点击它时访问GooglePlay商店。我看到了我使用的代码Firebasecon 查看详情
我可以从通知中启动对话活动,使其显示在我的应用程序窗口上而不是空白背景上吗?
】我可以从通知中启动对话活动,使其显示在我的应用程序窗口上而不是空白背景上吗?【英文标题】:CanIlaunchadialogactivityfromanotificationsoitappearsovermyapp\'swindowinsteadofonablankbackground?【发布时间】:2020-02-2216:06:51【问题描述】:我... 查看详情
推送通知打开一个新活动
...【问题描述】:我正在使用OneSignal推送通知服务。当用户点击推送通知时,它会自动打开一个名为“Main2Activity.java”的新活动。当用户点击通知时,代码正在工作并打开一个新的活动。但是如果我发送另一个推送通知,当用户点... 查看详情
如何清除从 Android 中点击通知打开的先前活动?
】如何清除从Android中点击通知打开的先前活动?【英文标题】:HowcanIclearpreviousactivitywhichisopenfromthetaponnotificationinAndroid?【发布时间】:2020-02-1713:45:18【问题描述】:有一个提醒应用程序。现在用户创建了5个提醒。R1、R2、R3、R4... 查看详情
当用户点击 Parse.com 通知时打开活动
】当用户点击Parse.com通知时打开活动【英文标题】:OpenactivitywhenusertapsonParse.comnotification【发布时间】:2015-03-1217:01:24【问题描述】:我正在使用Parse.com接收通知,我想在用户点击传入通知时打开MessagesActivy。目前,当我点击通... 查看详情
在alarmmanager中点击通知后打开活动并广播Kotlin
】在alarmmanager中点击通知后打开活动并广播Kotlin【英文标题】:OpenactivityafterclickinnotificationinalarmmanagerandbroadcastKotlin【发布时间】:2020-02-1110:28:03【问题描述】:我以这种方式在我的应用程序上每7小时设置一次通知:alarmManager=ge... 查看详情
关闭打开的活动,因此主要活动可以更新片段而不会在Android中丢失状态
】关闭打开的活动,因此主要活动可以更新片段而不会在Android中丢失状态【英文标题】:Closeopenedactivity,somainactivitycanupdatefragmentwithoutstatelossinAndroid【发布时间】:2020-03-3117:23:07【问题描述】:我目前有两个活动,第一个在打开... 查看详情
Android中的前台通知启动新的活动(通过pendingIntent)而不是现有的
】Android中的前台通知启动新的活动(通过pendingIntent)而不是现有的【英文标题】:ForegroundNotificationinAndroidstartsnewActivity(viapendingIntent)insteadofexistingone【发布时间】:2012-03-2619:55:08【问题描述】:我有一个音乐流应用程序,我想... 查看详情
如何打开另一个活动而不会丢失主要活动布局上的项目?(代码片段)
我有两个空的活动名称主要活动和另一个活动。它们都有不同的内容。在主要活动布局上,我有一个按钮和一个横幅广告。在我的另一个活动布局上,我有一个填充布局宽度和高度的imageview。我的问题是,如何在主布局上用我的... 查看详情