根据
Google有关广播的页面:
如果您不需要向应用程序外部的组件发送广播,则可以使用Support库中提供的LocalBroadcastManager发送和接收本地广播。 LocalBroadcastManager更加高效(无需进程间通信),并允许您避免考虑其他应用程序能够接收或发送您的广播所涉及的任何安全问题。 本地广播可用作应用程序中的通用发布/订阅事件总线,而无需考虑系统范围广播的任何开销。
因此,建议使用LocalBroadcastManager而不是BroadcastReceiver。
在FirebaseMessagingService类中,您将从FCM(Firebase Cloud Messaging)接收通知并使用:
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
向MainActivity发送消息:
public class AppFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
try {
String messageFrom = remoteMessage.getFrom();
String messageBody = (remoteMessage.getNotification() != null ? remoteMessage.getNotification().getBody() : null);
Map<String, String> messageData = remoteMessage.getData();
NotificationInfo notificationInfo = new NotificationInfo(messageData.get(message));
notifyMainActivity(notificationInfo);
showNotification(notificationInfo);
}
private void notifyMainActivity(NotificationInfo notificationInfo) {
Intent intent = new Intent();
intent.setAction(Constants.BROADCAST_MESSAGE_NOTIFICATION_RECEIVED);
intent.putExtra(Constants.PARAM_NOTIFICATION_INFO, notificationInfo);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
private void showNotification(NotificationInfo notificationInfo) {
Intent intentNotificationClicked = new Intent(this, MainActivity.class);
intentNotificationClicked.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentNotificationClicked.putExtra(Constants.PARAM_NOTIFICATION_INFO, notificationInfo);
PendingIntent resultIntentNotificationClicked = PendingIntent.getActivity(this, 0, intentNotificationClicked, PendingIntent.FLAG_ONE_SHOT);
String title = "MY APPLICATION";
String message = notificationInfo.message;
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification_icon)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(notificationSoundURI)
.setContentIntent(resultIntentNotificationClicked);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = Integer.parseInt(new SimpleDateFormat("HHmmssSSS").format(new Date()));
notificationManager.notify(notificationID, mNotificationBuilder.build());
}
}
在MainActivity中,您可以创建BroadcastReceiver来接收消息:
public class MainActivity extends AppCompatActivity {
private NotificationBroadcastReceiver mNotificationBroadcastReceiver = null;
private IntentFilter mIntentFilter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
this.mNotificationBroadcastReceiver = new NotificationBroadcastReceiver(this);
this.mIntentFilter = new IntentFilter(Constants.BROADCAST_MESSAGE_NOTIFICATION_RECEIVED);
}
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(this.mNotificationBroadcastReceiver, this.mIntentFilter);
}
@Override
protected void onPause() {
if (this.mNotificationBroadcastReceiver != null) {
unregisterReceiver(mNotificationBroadcastReceiver);
this.mNotificationBroadcastReceiver = null;
}
super.onPause();
}
private class NotificationBroadcastReceiver extends BroadcastReceiver {
WeakReference<MainActivity> mMainActivity;
public NotificationBroadcastReceiver(MainActivity mainActivity) {
this.mMainActivity = new WeakReference<>(mainActivity);
}
@Override
public void onReceive(Context context, Intent intent) {
MainActivity mainActivity = mMainActivity.get();
if (mainActivity != null) {
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(Constants.PARAM_NOTIFICATION_INFO)) {
NotificationInfo notificationInfo = (NotificationInfo) extras.getSerializable(Constants.PARAM_NOTIFICATION_INFO);
mainActivity.notificationReceived(notificationInfo);
}
}
}
}
public void notificationReceived(@NonNull final NotificationInfo notificationInfo)
{
}
}
以下是代码中引用的类:
public class Constants {
public static final String NOTIFICATION_BROADCAST_RECEIVER_MESSAGE_RECEIVED = "com.mycompany.myapp.NOTIFICATION_RECEIVED";
public static final String PARAM_NOTIFICATION_INFO = "NotificationContent";
}
public class NotificationInfo implements Serializable {
public String message;
public NotificationInfo() { }
public NotificationInfo(String message) { this.message = message; }
}
就是这样!