gpt4 book ai didi

java - 在 onMessageReceived() 中执行操作 - Firebase 云消息传递

转载 作者:行者123 更新时间:2023-12-02 10:19:49 24 4
gpt4 key购买 nike

我试图在监听 onMessageReceived() 中的有效负载后执行一个操作,我想要实现的操作在其方法中有一个 ProgressBar ,因此它会抛出一个空指针异常。

下面是我的方法onMessageReceived()

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getNotification() != null) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
public void run() {
roomAvailabilityHelper = new RoomAvailabilityHelper("");
roomAvailabilityHelper.endCurrentMeeting(new RoomAvailabilityActivity());
roomAvailabilityHelper.autoRefreshTimer(new RoomAvailabilityActivity());
Toast.makeText(getApplicationContext(),
"Am working", Toast.LENGTH_SHORT).show();
Log.d("Updated ->","Calendar updated");
}
});
}
}

然后,autoRefreshTimer()endCurrentMeeting() 在 RoomAvailabilityHelper` 类中可用。以下是方法。

 public static void endCurrentMeeting(RoomAvailabilityActivity activity) {
activity.progressBar.setVisibility(VISIBLE);
RoomAvailabilityHelper.updateRoomCalendarEvents(activity.mDisposables,
false, activity, 2000);
activity.runOnUiThread(() -> new Handler().postDelayed(() -> {
MeetingRoomDetailFragment meetingRoomDetailFragment = (MeetingRoomDetailFragment)
activity.getSupportFragmentManager()
.findFragmentById(R.id.frame_room_availability_details);
if (meetingRoomDetailFragment != null && activity.sharedPreferences.getString(
CountryActivity.SHARED_ROOM_CHECKED_IN, null) == null) {
meetingRoomDetailFragment.displayCheckInScreen();
}
activity.progressBar.setVisibility(View.GONE);
}, 3000));
}


public static void autoRefreshTimer(RoomAvailabilityActivity activity) {
final int timedPeriod = 300000;
final Handler handler = new Handler();
handler.postDelayed(() -> {
endCurrentMeeting(activity);
autoRefreshTimer(activity);
}, timedPeriod);
}

下面是错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.andela.mrm.room_availability.RoomAvailabilityHelper.endCurrentMeeting(RoomAvailabilityHelper.java:280)
at com.andela.mrm.notifications.Notifications$1.run(Notifications.java:40)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6939)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)

所以我正在寻找一种在onMessageReceived()中连接Activity的方法,但我无法得到它。这就是为什么由于 ActivityProgressBar 上出现空指针异常

最佳答案

我建议使用 LocalBroadcastManager 。因此,当您在 Firebase 服务文件中的 onMessageReceived() 中收到事件时,它会触发 Activity 中的函数 并在 Activity 中调用您的函数。

ProgressBarActivity

private BroadcastReceiver pushMessageBroadCastReceiver;
//here receive the broadcast from Firebase message,then your thing

//add this inside onCreate()
pushMessageBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AppConfig.PUSH_NOTIFICATION)) {

//Here your receive the data from onMessageReceived()
//do your thing here
Log.d("Activity","Is triggerred here")
}
}
};

@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(pushMessageBroadCastReceiver,
new IntentFilter(AppConfig.PUSH_NOTIFICATION));
}

@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(pushMessageBroadCastReceiver);
}

在你的AppConfig类中(如果没有,创建一个)

// broadcast receiver intent filters
public static final String PUSH_NOTIFICATION = "pushNotification";

在您的onMessageReceived()中添加以下代码,从这里触发您的 Activity 中的LocalBroadcastManager:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

//here you trigger the LocalBroadcastManager in your activity
Intent pushNotification = new Intent(AppConfig.PUSH_NOTIFICATION);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

}

关于java - 在 onMessageReceived() 中执行操作 - Firebase 云消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54417110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com