推送通知它打开我的第一个Activity而不是showNotification()方法

时间:2017-07-27 09:10:34

标签: android firebase android-intent android-activity android-notifications

我遇到了问题,可能会发生很多人,当我尝试在 MyFireBaseMessagingService

中启动showNotification方法中定义的活动时

重要提示: 我使用共享首选项配置我的应用程序, IntroActivity(Splash)只显示一次

流量初始值 IntroActivity(Splash) - > MainActivity(在导航抽屉菜单的默认片段选项中)

第二次运行应用程序: MainActivity(在导航抽屉菜单中的默认片段选项中)

我向您展示了我的代码,我非常感谢您的帮助,因为我需要解决此问题,如果您需要更多信息,请告诉我。

  

案例I(正确的流程)

当我在后台运行应用程序时,通知会显示在任务栏上,在我单击顶部栏中的通知后,应用程序在指定的活动 DetailNoticiaEventoActivity 中打开并显示正确的信息。

  

案例II(问题流程)

但是当我关闭应用程序并完成它时,通知会显示在任务栏上,但是当我单击它时,应用程序在 MainActivity 中打开,而不显示活动 DetailNoticiaEventoActivity < / strong>首先是我想要的

  

案例III(问题流程)

有时即使应用程序在后台,通知也会将我重定向到MainActivity

  

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pe.edu.usmp.fiausmp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- SPLASH ACTIVITY -->
        <activity
            android:name="pe.edu.usmp.fiausmp.Activities.IntroActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- END SPLASH ACTIVITY -->

        <!-- MAINACTIVITY WITH NAV DRAWER -->
        <activity android:name="pe.edu.usmp.fiausmp.Activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- END MAINACTIVITY WITH NAV DRAWER -->

        <!-- ACTIVITY SHOWED BY NOTIFICATION METHOD -->
        <activity android:name="pe.edu.usmp.fiausmp.Activities.DetalleNoticiaEventoActivity"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <!-- END ACTIVITY SHOWED BY NOTIFICATION METHOD -->

        <!--  FIREBASE CLASS-->
        <service android:name="pe.edu.usmp.fiausmp.Listeners.MyFireBaseMessagingService">
            <intent-filter>
                <action android:name= "com.google.firebase.MESSAGING_EVENT"></action>
            </intent-filter>
        </service>

        <service android:name="pe.edu.usmp.fiausmp.Listeners.MyFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
            </intent-filter>
        </service>
        <!--  END FIREBASE CLASS-->

    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"
        />

</manifest>
  

MyFireBaseMessagingService - showNotificationMethod()

这是我的全班,我用Intent开始一个活动,并用这个设置PendindIntent。我在任何设备上使用getNotification()来显示通知,并使用getData()来获取自定义信息,如Ticker,BigTextStyle和Sound。

注意:注释行反映了您测试的解决方案以及本出版物的意见和建议

public class MyFireBaseMessagingService extends FirebaseMessagingService{

    private static final String TAG = "FIA USMP";

    int contador = 0;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage == null){
            return;
        }

        Log.e(TAG, "From: " + remoteMessage.getFrom());
        Log.e(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
        Log.e(TAG, "Notification Message Data: " + remoteMessage.getData());

        if (remoteMessage.getData().size() > 0){

                JSONObject json = new JSONObject(remoteMessage.getData());
                sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), json);

        }
    }

    //This method is only generating push notification
    private void sendNotification(String title, String messageBody, JSONObject json) {

        int cod_not_eve = 0;
        String cod_mod = "";
        String titulo = "";
        String resumen = "";
        String detalle = "";

        try {
            cod_not_eve = Integer.parseInt(json.getString("cod_not_eve"));
            cod_mod = json.getString("cod_mod");
            titulo = json.getString("titulo");
            resumen = json.getString("resumen");
            detalle = json.getString("detalle");

        }catch (JSONException e){
            Log.e(TAG, "JSONException: " + e.getMessage());
        }catch (Exception e){
            Log.e(TAG, "Exception2: " + e.getMessage());
        }

        Intent intent = new Intent(this, DetalleNoticiaEventoActivity.class);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("cod_not_eve", cod_not_eve);
        intent.putExtra("cod_mod", cod_mod);
        //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        //        PendingIntent.FLAG_ONE_SHOT);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        //PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        //        PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                //.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setContentTitle(title)
                .setContentText(messageBody)
                //.setStyle(new NotificationCompat.BigTextStyle().bigText(detalle))
                .setAutoCancel(true)
                //.setVibrate(new long[] {0, 1000, 200,1000 })
                //.setLights(Color.parseColor("#00BCD4"), 500, 500)
                //.setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);
                //.setTicker(titulo);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(contador, notificationBuilder.build());
        Log.d("","MyFireBaseMessagingService.sendNotification.contador"+contador);
        contador++;
    }

}

1 个答案:

答案 0 :(得分:0)

Intent intent = new Intent(this, DetalleNoticiaEventoActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);    

pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);