我发现在Android 6.0.1上,当我们的应用程序处于后台并且通知来自Firebase时,如果我点击通知,我们的应用程序无法启动......只有当我的应用程序在前台时,我才能获得消息接收,并且我正在处理以下代码。 每当用户单击通知时,都会调用 SplashActivity
@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);
}
但是,如果我的应用程序被杀死,如果收到通知并且如果我单击它,我想从我的 SplashActivity 中获取数据,如下所示
Intent sourceIntent = getIntent();
if (sourceIntent != null) {
Bundle bundle = sourceIntent.getExtras();
if (bundle != null) {
String strHref = bundle.getString(Constants.MyFirebase.HREF, "");
但是当我单击通知(默认(时,没有任何反应,我的应用程序无法启动
AndroidMenifest.xml如下所示
<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>