Android 如何从Firebase消息传递服务类调用MainActivity类中的方法?
我一直在尝试从消息传递服务调用MainActivity中的方法。我试过几种方法,都是从StackOverflow开始的,但似乎都不管用。以下是我迄今为止所做的工作 我只想在收到通知后显示一个对话框 这是MessagingServiceclass中的方法Android 如何从Firebase消息传递服务类调用MainActivity类中的方法?,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我一直在尝试从消息传递服务调用MainActivity中的方法。我试过几种方法,都是从StackOverflow开始的,但似乎都不管用。以下是我迄今为止所做的工作 我只想在收到通知后显示一个对话框 这是MessagingServiceclass中的方法 sendNotification(title, message); Intent pushNotification = new Intent("PushNotification"); pushNot
sendNotification(title, message);
Intent pushNotification = new Intent("PushNotification");
pushNotification.putExtra("message", message);
pushNotification.putExtra("title", title);
pushNotification.putExtra("code", code);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
savetodatabase(title,message);
最后一行是将通知保存到SQL数据库,并且运行良好,这意味着上面的代码正在执行
在我的MainActivity类中,我使用下面的代码调用BroadcastReceiver
BroadcastReceiver mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Called", Toast.LENGTH_SHORT).show();
if (intent.getAction().equals(NOTIFICATION)) {
String title = intent.getStringExtra("title");
String message = intent.getStringExtra("message");
String code = intent.getStringExtra("code");
showDialog(title, message, code);
}
}
};
但是,不会调用OnReceive中的方法
我还尝试复制我希望在消息传递类中收到通知后执行的方法,但仍然没有调用该方法
我哪里出错了?显示您注册的代码
mrRegistrationBroadcastReceiver
。显示您注册的代码mrRegistrationBroadcastReceiver
。