在Firebasemessagingservice接收消息时弹出消息



我有一个Android应用程序。因为我有一个使用firebase进行通知。当收到FirebaseMessagingService类中的消息时,我需要在主页上显示弹出消息

firebasemessagingservice

String actiondata = remoteMessage.getData().get("action");
if(actiondata.equals("booking")){
        Intent myIntent = new Intent("com.driver.approverequest");
        myIntent.putExtra("contentdata",contentdata);
        myIntent.putExtra("requestid",productId);
        this.sendBroadcast(myIntent);
        Log.d(TAG, "Message data : athira send " + message);
    }

homeactitivity

 public BroadcastReceiver myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getStringExtra("action");
        try {
            JSONObject mJsonObject = new JSONObject(intent.getExtras().getString("contentdata"));
            Log.d("hi...........",intent.getExtras().getString("contentdata"));
            Toast.makeText(getApplicationContext(),"hi",Toast.LENGTH_LONG).show();
            StartTrackingDriver();
        } catch (Exception e){
        }
    }
};
@Override
protected void onResume() {
    super.onResume();
    registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));
}
@Override
protected void onPause() {
    super.onPause();
    try {
        unregisterReceiver(myReceiver);
    }catch (Exception e){}
}

@aryaFCM致力于以下概念。

1个应用在前面2个应用在后台

1当应用在前面时,无论您从服务器收到的JSON有效载荷如何

2当您的应用在后台时,仅当您的JSON不包含"通知"键时,才会调用OnReceive方法。如果您的有效载荷包含通知密钥,则Firebase SDK将管理全部。

OnCreate()

中播放注册广播
registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));

onDestroy()

中取消注册
unregisterReceiver(myReceiver);

在您的活动中进行活动时,Onpause在Onpause中都会取消广播。

您正在从意图中获取字符串" action",但从未将价值设置为意图。

String action = intent.getStringExtra("action");

因此,将字符串设置为先意向。

myIntent.putExtra("action",actiondata);

编辑register之前尝试使用LocalBroadcastManager.getInstance(mContext)unregister&sendbroadcast()

LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));
LocalBroadcastManager.getInstance(context).unregisterReceiver(myReceiver);
LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
String actiondata = remoteMessage.getData().get("action");
 if(actiondata.equals("booking")){
     Intent myIntent = new Intent("com.driver.approverequest");
     myIntent.putExtra("contentdata",contentdata);
     myIntent.putExtra("requestid",productId);
     LocalBroadcastManager.getInstance(this).sendBroadcast(myIntent);
     Log.d(TAG, "Message data : athira send " + message);
 }
//In your HomeActivity
@Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
  BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
  @Override
     public void onReceive(Context context, Intent intent) {
         String data = intent.getStringExtra("contentdata");
         String id = intent.getStringExtra("requestid");
 //Write your pop up message logic here
  }
     }
 @Override
 protected void onResume() {
 LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,new IntentFilter("com.driver.approverequest"));
        super.onResume();
 }
 @Override
 protected void onPause() {  
  LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
     super.onPause();
 }

相关内容

  • 没有找到相关文章

最新更新