0

アクティビティFirebase Cloud Messagingでfragmentを初期化しようとしています。FCMアプリケーションをデバイス上で実行していないときのアクティビティとコールフラグメントのオープン

フラグメントコールは、アプリケーションが開いているときに機能しますが、アプリケーションを閉じると、プッシュ通知はフラグメントではなくアクティビティのみを開きます。

MyActivity.java私は、デバッグ(または解決)この問題に何ができるか

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //All my initial code 

     Intent i = getIntent(); 
     Bundle extras = i.getExtras(); 

     if(extras != null) { 
      String push = extras.getString("push"); 
      if (push != null) { 
       Integer id = Integer.parseInt(extras.getString("id")); 
       goToDetalleDenuncia(id); 
      } 
     }else 
      goToMenu(); 
    } 

? アプリケーションがこの時点で閉じられているため、Logcatを使用できません(少なくともEclipseを使用しています)

答えて

4

最後に、私の問題の解決策が見つかりました。

まず、FCM通知はフラグメントを直接呼び出すことはできません。 click_actionパラメータを使用する必要があります。

第1 たManifest.xmlここ

<activity 
      android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"> 
      <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> 
      <intent-filter> 
       <action android:name="sm.boson.com.smd.MAIN" /> 
       <category android:name="android.intent.category.DEFAULT"/> 
      </intent-filter> 
     </activity> 

、あなたはaction android:nameintent-filterセクションに追加し、名前を指定する必要があります。私の場合、私は同じパッケージ名を ".MAIN"(MainActivityのシンボルを表していますが、別の名前を使用することもできます)という名前を使用しました。私Intentの宣言で

MyFirebaseMessagingService.java

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 

     String id = remoteMessage.getData().get("tag") ; 

     String click_action = remoteMessage.getNotification().getClickAction(); 

     Intent intent = new Intent(click_action); 
     intent.putExtra("id", id); 
     intent.putExtra("push", "true"); 

     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle(getResources().getString(R.string.app_name)); 
     notificationBuilder.setContentText(remoteMessage.getNotification().getBody()); 

     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     notificationBuilder.setContentIntent(pendingIntent); 
     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 
    } 

私は活動にいくつかのパラメータを送信するためにExtraを追加しました。

MyActivity.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     //ALL THE LOGIC OF YOU ONCREATE EVENT 

     Intent i = getIntent(); 
     Bundle extras = i.getExtras(); 

     if(extras != null) { 
      String push = extras.getString("push"); 
      if (push != null) { 
       Integer id = Integer.parseInt(extras.getString("id")); 
       goToDetalleDenuncia(id); 
      }else if ( extras.getString("tag") != null ){ 
       Integer id = Integer.parseInt(extras.getString("tag")); 
       goToDetalleDenuncia(id); 
      }else 
       goToMenu(); 
     }else 
      goToMenu(); 
    } 

まあ、アプリケーションが実行されているとき、あなたは最初if文でパラメータを取得し、それらを取得することができます。extras.getString("push")

しかし、あなたのアプリケーションがある場合バックグラウンドで実行している(または閉じている)場合は、次のようにパラメータを取得する必要があります。

Integer.parseInt(extras.getString("tag")); 

なぜ「タグ」ですか? 私のMyFirebaseMessagingServiceで私は "タグ"を取得し、私はExtraの私の意図のための "ID"キーに入れました。

など私のサーバーからの私のJSON:

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "priority" : "normal", 
    "notification" : { 
    "body" : "This week's edition is now available.", 
    "title" : "Mytitle", 
    "icon" : "new" 
    }, 
    "data" : { 
    "tag" : "3.21.15", 
    } 
} 

、このように、今私は、バックグラウンドで動作しているとき、それが閉じていたときに、アプリケーションが、実行されているときFCMを使用して、特定のFragmentを開くことができます。

希望すると、これが役立ちます。

+0

それは私を助けました。ありがとう:* –

+0

あなたのサービスで;)@MahboobehMohammadi – MrMins

関連する問題