アプリケーションがバックグラウンドで動作しているときの意図からエクストラを得るカント

Rifki Maulana:

私は通知がクリックされたときに、私のMainActivityで関数を実行してみてください。機能は、私が意図エキストラを入れていることのデータを必要とします。

私は、アプリケーションが関数が実行されて実行されているが、アプリケーションがバックグラウンドにあるとき、私は、通知をクリックすると、関数が実行されていない通知をクリックしたときに問題があります。私はそれをチェックしましたし、アプリケーションがバックグラウンドにあるとき、私は意図エキストラに置くことをデータが空であるためです。

どのように私はこの問題を解決することができますか?ありがとう!

これは私が受け取る応答です。

{
    "to":"blablabla",
    "notification": {
        "body":"Sentiment Negative from customer",
        "title":"Mokita"
    },
    "data" : {
        "room_id":1516333
    }
}

これは私のある通知コード:

public void onMessageReceived(RemoteMessage message) {
    super.onMessageReceived(message);
    Log.d("msg", "onMessageReceived: " + message.getData().get("room_id"));
    String roomId = message.getData().get("room_id");

    Intent intent = new Intent(this, HomePageTabActivity.class);
    intent.putExtra("fromNotification", true);
    intent.putExtra("roomId", roomId);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String channelId = "Default";
    NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(message.getNotification().getTitle())
            .setContentText(message.getNotification().getBody())
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }

    manager.notify(0, builder.build());
}

}

そして、これはある機能とどのように私はMainActivityでそれを実行します:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    onNewIntent(getIntent());
}

@Override
    public void onNewIntent(Intent intent){
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("fromNotification") || extras.containsKey("roomId")) {
                openChatRoom(Long.valueOf(extras.getString("roomId")));
            }else if(extras.containsKey("fromNotification") && extras.containsKey("roomId")){
                openChatRoom(Long.valueOf(extras.getString("roomId")));
            }else{
                Log.e("EXTRAS room",""+extras.getString("roomId"));
                Log.e("EXTRAS STATUS",""+extras.getBoolean("fromNotification"));
            }
        }else{
            Toast.makeText(HomePageTabActivity.this,"Empty",Toast.LENGTH_SHORT).show();
        }
    }


public void openChatRoom(long roomId){
        Log.d("LONG ROOM",""+roomId);
        QiscusRxExecutor.execute(QiscusApi.getInstance().getChatRoom(roomId),
        new QiscusRxExecutor.Listener<QiscusChatRoom>() {
            @Override
            public void onSuccess(QiscusChatRoom qiscusChatRoom) {
                startActivity(GroupRoomActivity.
                        generateIntent(HomePageTabActivity.this, qiscusChatRoom));
            }
            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
Amad YUS:

通知メッセージとデータメッセージ:Firebaseは、メッセージの2種類があります。あなたはFCM SDKは、独自でメッセージを処理したい場合は、使用する必要がありますnotificationアプリがアクティブでない場合は、FCMが使用するnotificationメッセージを表示するために身体を。この状態で、onMessageReceivedまた起動されません。あなたはアプリがメッセージを処理したい場合は、使用する必要がありますdataあなたはからプッシュ通知ペイロードを変更する必要があります

{
   "message":{
   "token":"xxxxx:...",
   "notification":{
          "title":"Your title",
          "body":"Your message"
      }
   }
}

{
   "message":{
   "token":"xxxxx:...",
   "data":{
          "title":"Your title",
          "body":"Your message",
          "fromNotification":"true",
          "roomId":"123"
      }
   }
}

あなたはまたでメッセージを処理する必要onMessageReceived(RemoteMessage remoteMessage)に応じて。あなたはで通知行動について読むことができる通知とデータメッセージ

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=185303&siteId=1