当我单击默认Firebase通知Android 6.0.1时,应用程序未启动

时间:2018-08-08 12:27:04

标签: android firebase firebase-cloud-messaging

我发现,在 Android 6.0.1 上,当我们的应用程序处于后台并且通知从firebase发出时,如果我点击通知,则我们的应用程序不会启动...。在前台,我可以获取onMessageReceived,并且正在处理以下代码。 每当用户单击通知时,都会调用 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>

1 个答案:

答案 0 :(得分:0)

您需要从活动标签中删除该标签

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>