我有一个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);
}
家庭活动
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){}
}
答案 0 :(得分:0)
在OnCreate()
中注册广播
registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));
在onDestroy()
上取消注册
unregisterReceiver(myReceiver);
每当活动进入后台时,在onPause中取消注册将取消广播的注册。
AND
您从意图中获得了字符串“ action”,但从未将值设置为intent。
String action = intent.getStringExtra("action");
因此,请先将字符串设置为intent。
myIntent.putExtra("action",actiondata);
编辑
尝试在LocalBroadcastManager.getInstance(mContext)
,register
和unregister
sendbroadcast()
LocalBroadcastManager.getInstance(context).registerReceiver(myReceiver, new IntentFilter("com.driver.approverequest"));
LocalBroadcastManager.getInstance(context).unregisterReceiver(myReceiver);
LocalBroadcastManager.getInstance(context).sendBroadcast(myIntent);
答案 1 :(得分:0)
@Arya Fcm致力于以下概念。
1个应用位于最前面 2个应用程序处于后台
1当应用程序位于最前面时,无论从服务器接收到的json有效负载如何,每次都会调用onReceive方法
2当您的应用程序在后台运行时,仅当您的json不包含“通知”键时,才会调用onReceive方法。如果您的有效负载包含通知密钥,则firebase sdk将管理所有内容。
答案 2 :(得分:0)
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();
}