当我单击默认firebase通知Android 6.0.1时,应用程序未启动
我发现在安卓6.0.1上,当我们的应用程序在后台,通知来自firebase,如果我点击通知,我们的应用程序不会启动。。。。只有当我的应用程序在前台时,我才能获得onMessageReceived,并且我正在处理下面的代码。 每当用户单击通知时,将调用SplashActivity当我单击默认firebase通知Android 6.0.1时,应用程序未启动,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我发现在安卓6.0.1上,当我们的应用程序在后台,通知来自firebase,如果我点击通知,我们的应用程序不会启动。。。。只有当我的应用程序在前台时,我才能获得onMessageReceived,并且我正在处理下面的代码。 每当用户单击通知时,将调用SplashActivity @Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(re
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.e("dataChat",remoteMessage.getData().toString());
try
{
Map<String, String> params = remoteMessage.getData();
JSONObject jsonObject = new JSONObject(params);
String strHref = jsonObject.optString(Constants.MyFirebase.HREF);
if(strHref!=null && !strHref.equals("")){
createNotification(remoteMessage.getNotification().getTitle(), strHref);
}
}catch (Exception e){
e.printStackTrace();
}
}
private void createNotification(String title, String href){
Intent intent = new Intent(this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Constants.MyFirebase.HREF, href);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification = new NotificationCompat.Builder(this, "MyApp")
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText("")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Channleld",
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notification);
}
但是当我点击通知(默认)时,我的应用程序没有启动
AndroidMenifest.xml如下所示
Intent sourceIntent = getIntent();
if (sourceIntent != null) {
Bundle bundle = sourceIntent.getExtras();
if (bundle != null) {
String strHref = bundle.getString(Constants.MyFirebase.HREF, "");
<activity
android:name=".activities.SplashActivity"
android:launchMode="singleTop"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
您需要从活动标记中删除此标记
android:launchMode="singleTop"
正如你已经用过的
intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK)代码>
在等待通知的意图中
也要用这个
<activity
android:name=".activities.SplashActivity"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
也不需要使用这个
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
删除此标签android:launchMode=“singleTop”谢谢,让我检查一下我已经删除了,它不工作了我已经测试了没有默认意图过滤器,它不工作了,这就是为什么我添加了右键,你也试过使用PendingEvent.FLAG\u CANCEL\u CURRENT吗?但是当我们的应用程序被终止时,onMessageReceived不会调用右键,因此,我们的通知不会来自我们的代码。无论应用程序是否正在运行或终止,都会调用onMessageReceived,这就是广播接收器用于我们的功能。