当应用程序处于前台而未进入BroadcastReceiver时,从FCM广播数据

时间:2017-08-04 10:43:10

标签: android firebase broadcastreceiver firebase-cloud-messaging

我在我的应用程序中使用firebase推送通知,如果应用程序在前台,我正在接收消息时向另一个活动广播意图。但我没有在定向活动中写入BroadcastReceiver的意图。

发送意图就像

 @Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    if (isAppIsInBackground(this)){ 
        int dbcount = Integer.parseInt(dbHelper.getPushCount());
        int i = count;
        int current = dbcount +4;
        dbHelper.updatePushCount(Integer.toString(current));
    } else {
        Intent intent = new Intent(this, NavigationHome.class);
        intent.setAction("new.pushMessage");
        intent.putExtra("count", count.toString());
        sendBroadcast(intent);
    }
}

和我希望得到意图的活动中的接收者是

public class NavigationHome extends BaseActivity
    implements NavigationView.OnNavigationItemSelectedListener {

int count=0;
Context context;
private BroadcastReceiver LandingBr = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("received",""+1);
        NavigationHome.this.update();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation_home);

    dbhelper = new DBHelper(this);

...................

更新()函数未被调用,如何在Activity中获取意图?

1 个答案:

答案 0 :(得分:0)

在活动中创建扩展BroadcastReceiver的MyReceiver类。

class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // do your stuff here
    }
}

并在特定活动的onCreate()中注册您的BroadcastReceiver。使用以下代码。

MyReceiver myReceiver =new MyReceiver();
IntentFilter intentFilter= new IntentFilter(Constants.ACTION);
registerReceiver(myReceiver,intentFilter);