Android 在应用程序打开时检查通知中的消息

Android 在应用程序打开时检查通知中的消息,android,notifications,Android,Notifications,我正在做一个拼车应用程序。为了让人们进入骑乘设施,我让驾驶员在想要启动骑乘设施时向骑乘者发送通知。之后,当骑手点击通知时,SharedReferences上的布尔值发生变化,骑乘开始。但是,当骑手打开应用程序,没有点击任何通知,它不会发生,这是合理的,因为我没有做任何改变。这就是为什么我想在用户每次打开应用程序时检查通知,但我不知道如何做 我的听众↓ public class FcmMessagingService extends FirebaseMessagingService { @Over

我正在做一个拼车应用程序。为了让人们进入骑乘设施,我让驾驶员在想要启动骑乘设施时向骑乘者发送通知。之后,当骑手点击通知时,SharedReferences上的布尔值发生变化,骑乘开始。但是,当骑手打开应用程序,没有点击任何通知,它不会发生,这是合理的,因为我没有做任何改变。这就是为什么我想在用户每次打开应用程序时检查通知,但我不知道如何做

我的听众↓

public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        JSONObject json = new JSONObject(remoteMessage.getData());
        try {
            sendNotification(json.getString("message").split("Message data payload: ")[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void handleIntent(Intent intent) {
    super.handleIntent(intent);
    String msg = intent.getExtras().get("message").toString();
    System.out.println("Guia: " + msg);
    if (msg.contains("IniciarCaronaAgora")) {
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("emCarona", "true").apply();
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString("emCaronacid", msg.split("IniciarCaronaAgora cid:")[0]).apply();
    }
}

private void sendNotification(String message) {
    Intent intent = new Intent(this, ListaCaronasFragment.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("fromNotification", true);
    intent.putExtra("message", message);
    startActivity(intent);
}
如果有人收到通知而应用程序未打开,则普通侦听器将工作,而我的侦听器将不工作。这就是为什么我在主活动中有一小部分代码,如果有任何意图,它将打开一个警报框。↓

if (getIntent().getExtras()!= null){
        String message = getIntent().getExtras().get("message").toString();
        AlertDialog ad = new AlertDialog.Builder(ListaCaronasFragment.this).create();
        if (message.contains("IniciarCaronaAgora")) {
            PreferenceManager.getDefaultSharedPreferences(this).edit().putString("emCarona", "true").apply();
            PreferenceManager.getDefaultSharedPreferences(this).edit().putString("emCaronacid", message.split("IniciarCaronaAgora cid:")[0]).apply();
        }
        ad.setTitle("Aviso");
        ad.setMessage(message);
        ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        ad.show();
    }

在做研究时,我得出的结论是,很少有人想做我想做的事。它不是关于做无用的调用或发送错误类型的通知,而是没有人这样做并发布给开发人员。因此,我决定使用我的数据库,而不是通过通知(让某人搭便车)来实现这一点,这样可以保证数据的持久性和可靠性。

在backgtound中应用程序时,您无法管理通知?我使用的是标准方式。它通常显示应用程序图标和应用程序名称。我只是控制向用户显示消息。换句话说,我无法控制,直到现在,我还没有理由这么做。通常的答案是:你发送了错误的通知类型(多次询问,类似于onMessageReceived的东西没有被调用)。事实上,即使你试图澄清你的答案,我也不认为我得到了它,因为我没有反复询问收到的消息。我只想在每次打开应用程序时检查通知(检查是否有任何通知包含特定消息)。