2016-09-23 8 views
1

私はfcmを使用してアプリケーションで通知を配信しています。ユーザーが通知をクリックするたびにMsgViewActivityを起動します。これは、アプリがフォアグラウンドにあるときにはうまく動作しますが、アプリが実行されていないときは、私を主な活動に連れて行きます。また、データメッセージを使用しているので、onMessageRecivedはバックグラウンドでも呼び出されることに注意してください。FCMサービスからアクティビティを起動できません

はここにあなたが両方notification: {..}data: {..}ペイロードを持つメッセージを使用しているあなたの例からFirebaseMessagingService.class

public class FirebaseMessagingSer extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher); 

     Intent intent = new Intent(this, MsgViewActivity.class); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

     if(remoteMessage.getData().size()>0) { 
      String id = remoteMessage.getData().get("id"); 
      Bundle bundle = new Bundle(); 
      bundle.putString("id",id); 
      intent.putExtras(bundle); 
     } 
     PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
     notificationBuilder.setContentTitle("FCM NOTIFICATION"); 
     notificationBuilder.setContentText(remoteMessage.getNotification().getBody()); 
     notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
     notificationBuilder.setLargeIcon(bmp); 
     notificationBuilder.setAutoCancel(true); 
     notificationBuilder.setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0,notificationBuilder.build()); 


    } 
} 

答えて

1

の私のコードです。

これは、あなたのメッセージがnotification-messageとみなされ、その結果、方法がonMessageReceived()であることが、アプリケーションがフォアグラウンドにある場合にのみ実行されることを意味します。

  1. 使用データメッセージのみ:次の2つのソリューションを持っている

    (これはあなたの問題を説明します)。通知の中で何も送っていない:メッセージの一部。データペイロード内に追加のキー値として本文とタイトルを設定できます。

  2. click_action = "intent_to_activity2"パラメータで通知メッセージを使用できます。
    また、アクティビティマニフェストにインテントフィルタを追加する必要があります。

関連する問題