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

问题描述:

我正在做一个拼车应用程序。为了让乘客进入车内,我会让司机在他/她想要开始乘车时向乘客发送通知。之后,当车手点击通知时,在SharedPreferences中更改布尔值,并开始骑行。但是,当骑手打开应用程序时,如果没有点击任何通知,它就不会发生,因为我没有做任何改变它的事情是合理的。这就是为什么我想每次用户打开应用程序时都要检查通知的原因,但我不知道如何去做。在应用程序打开时检查通知中的消息

我一般的听众↓

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

在backgtound的应用程序中,你无法管理通知? –

+0

我用它的标准方式。它通常显示应用程序图标和应用程序名称。我只是控制向用户显示消息。换句话说,我不能控制,直到现在,我没有理由。 –

+0

像往常一样的答案是:你发送了一个错误的通知类型(问很多很多次,像* onMessageReceived不叫*) – Selvin

做了我的研究,我得出结论,很少有人试图做我想做的事。这不是关于做无用的调用或发送错误类型的通知,而是没有人执行并发布给开发人员。因此,我决定使用我的数据库来做到这一点,而不是通过通知(将某人放在一边)来做到这一点,这保证了数据的持久性和可靠性。