0

我有一個問題,也許有很多人在showNotification定義的活動的情況,以及當我嘗試在MyFireBaseMessagingService類開始在showNotification方法定義的活動推送通知它打開我的第一個活動,而不是()方法

重要:我配置我aplication與共享偏好,IntroActivity(濺)的表現只是一個時間

流量初始:IntroActivity(飛濺)- > MainActivity(在抽屜式導航菜單默認片段選項)

第二次運行應用程序:MainActivity(在默認片段選項在抽屜式導航菜單)

我向你展示我的代碼,我會非常感謝你的幫助,因爲我需要解決這個問題,如果你需要更多信息,請告訴我。

案例一(正確的流量)

當我在後臺應用程序,通知顯示在任務欄上,我點擊頂欄的通知後,該應用程序是在指定的開放活動使用正確的信息進行詳細的NiagaiaEventoActivity

案例二(問題流量)

但是當我關閉應用程序並完成它,通知顯示在任務欄上,但是當我點擊它,應用程序打開的MainActivity,並沒有表現出活性DetailNoticiaEventoActivity一審我怎麼想

案例三(問題流量)

有時即使應用程序在後臺,通知重定向我在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> 

MyFireBaseMessagin gService - showNotificationMethod()

這是我滿級,我開始與意圖的活動,並與這一個設定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++; 
    } 

} 
+0

嘗試添加intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)的意見和建議測試的解決方案;並刪除其他兩個。 –

+0

如何在服務器中發送通知,冷卻添加服務器代碼? –

+0

除'IntroActivity'之外,從所有''刪除''。不要使用PendingIntent.FLAG_ONE_SHOT。 –

回答

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); 
+0

我試過這個,但是我得到了同樣的錯誤,我不知道 –

+0

你是否在onMessageReceived()方法中獲得通知的響應? –

+0

是的,我在我的帖子中添加了我的onMessageReceived,這很奇怪,有時候通知會打開活動** DetailNoticiaEventoActivity **我想怎麼樣,但有時只是打開最初的**接口(MainActivity)**,我需要我的應用程序總是打開** PendingIntent Intent **中定義的活動 –

相關問題