onMessageReceived()でアクションを実行します-Firebase Cloud Messaging

Idrisスタック

onMessageReceived()でペイロードをリッスンした後にアクションを実行しようとしています。実装するアクションのメソッドにProgressBarがあるため、nullポインター例外がスローされます。

以下は私の方法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()、私はそれを取得することはできません。そのため、アクティビティが原因で、ProgressBarnullポインタ例外が発生ます

ケン

私が提案するのは使用ですLocalBroadcastManager。FirebaseonMessageReceived()サービスファイルでイベントを受信するActivity、で関数をトリガーし、で関数を呼び出しますActivity

あなたの中ActivityProgressBar

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()ここから、下のコードを追加しますが引き金と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);

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Firebase Cloud Messaging Statistics API

分類Dev

Firebase Cloud Messaging SystemUIが停止しました

分類Dev

Firebase Cloud Messagingは無料ですか?

分類Dev

Firebase Cloud Messagingは無料ですか?

分類Dev

Firebase Cloud Messaging認証は必要ですか?

分類Dev

JSON を Firebase Cloud Messaging に送信する

分類Dev

Firebase Cloud Messagingは、アプリケーションの実行時に通知しません

分類Dev

Notification from Firebase cloud messaging are received but they are not shown in the cloud messaging reports

分類Dev

Firebase Cloud Messaging - Error calling firebase.messaging()

分類Dev

新しいFirebase Cloud Messagingシステムの通知アイコン

分類Dev

Firebase Cloud Messaging - Managing Registration Tokens

分類Dev

Firebase Cloud Messaging AppDelegateエラー

分類Dev

Firebase Cloud Messaging notification not received on iOS

分類Dev

Firebase Cloud Messaging 通知キー名

分類Dev

Firebase Cloud MessagingはonDeletedMessages()をオーバーライドします

分類Dev

Firebase Cloud Messaging: App Server でのトークン更新の処理

分類Dev

Firebase Cloud MessagingはChromeでは機能しません(iOS)

分類Dev

Firebase Cloud Messagingはパッケージ名に依存しますか?

分類Dev

新しい Google Play アプリと Firebase Cloud Messaging

分類Dev

Firebase Cloud Messaging-登録トークンの管理

分類Dev

Firebase Cloud Messaging - Target to single device not visible in Firebase console

分類Dev

Firebase Cloud Messaging: Device Group sending support in Firebase Admin SDK

分類Dev

Firebase Cloud MessagingのAPIキーはどこにありますか?

分類Dev

Firebase Cloud Messagingにはサーバーが必要ですか?

分類Dev

Firebase Cloud Messaging には WAKE LOCK 権限が本当に必要ですか?

分類Dev

Firebase Cloud Messaging(FCM)で通知の重複を防ぐ方法

分類Dev

通知に Google App Engine と Firebase Cloud Messaging を使用する

分類Dev

Delphi Android Firebase Cloud Messaging:通知の受信時にアプリケーションがクラッシュする

分類Dev

Firebase Cloud Messaging-ログアウトの処理

Related 関連記事

  1. 1

    Firebase Cloud Messaging Statistics API

  2. 2

    Firebase Cloud Messaging SystemUIが停止しました

  3. 3

    Firebase Cloud Messagingは無料ですか?

  4. 4

    Firebase Cloud Messagingは無料ですか?

  5. 5

    Firebase Cloud Messaging認証は必要ですか?

  6. 6

    JSON を Firebase Cloud Messaging に送信する

  7. 7

    Firebase Cloud Messagingは、アプリケーションの実行時に通知しません

  8. 8

    Notification from Firebase cloud messaging are received but they are not shown in the cloud messaging reports

  9. 9

    Firebase Cloud Messaging - Error calling firebase.messaging()

  10. 10

    新しいFirebase Cloud Messagingシステムの通知アイコン

  11. 11

    Firebase Cloud Messaging - Managing Registration Tokens

  12. 12

    Firebase Cloud Messaging AppDelegateエラー

  13. 13

    Firebase Cloud Messaging notification not received on iOS

  14. 14

    Firebase Cloud Messaging 通知キー名

  15. 15

    Firebase Cloud MessagingはonDeletedMessages()をオーバーライドします

  16. 16

    Firebase Cloud Messaging: App Server でのトークン更新の処理

  17. 17

    Firebase Cloud MessagingはChromeでは機能しません(iOS)

  18. 18

    Firebase Cloud Messagingはパッケージ名に依存しますか?

  19. 19

    新しい Google Play アプリと Firebase Cloud Messaging

  20. 20

    Firebase Cloud Messaging-登録トークンの管理

  21. 21

    Firebase Cloud Messaging - Target to single device not visible in Firebase console

  22. 22

    Firebase Cloud Messaging: Device Group sending support in Firebase Admin SDK

  23. 23

    Firebase Cloud MessagingのAPIキーはどこにありますか?

  24. 24

    Firebase Cloud Messagingにはサーバーが必要ですか?

  25. 25

    Firebase Cloud Messaging には WAKE LOCK 権限が本当に必要ですか?

  26. 26

    Firebase Cloud Messaging(FCM)で通知の重複を防ぐ方法

  27. 27

    通知に Google App Engine と Firebase Cloud Messaging を使用する

  28. 28

    Delphi Android Firebase Cloud Messaging:通知の受信時にアプリケーションがクラッシュする

  29. 29

    Firebase Cloud Messaging-ログアウトの処理

ホットタグ

アーカイブ