【问题标题】:android firebase reg id recived but notification not show in phone displayandroid firebase reg id recived但通知未显示在手机显示屏中
【发布时间】:2017-08-22 11:08:17
【问题描述】:

我会尝试在我的手机托盘上显示个人单身通知,但我无法发财,请帮忙。

我在使用 FireBase 云消息传递时遇到问题,我从设备获取令牌并通过 Google Firebase 通知控制台发送通知测试,但是,通知从未被记录或推送到 android 虚拟设备。 FCM 的文档几乎就是我在下面的代码,而在使用 firebase 时,您还需要做些什么来获得推送通知。我已经完成了文档中指定的所有设置信息(build.gradle 添加、安装 google play 服务等),但仍然没有生成消息。我没有收到到 logcat 或设备的推送通知的代码有什么问题?请让我知道任何有帮助的进一步信息。谢谢。

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                                    FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);

                displayFirebaseRegId();

            } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) 
              {
                String message = intent.getStringExtra("message");

                Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();

                txtMessage.setText(message);
            }
        }
    };

    displayFirebaseRegId();
}

    private void displayFirebaseRegId() {
    SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
    String regId = pref.getString("regId", null);

    Log.e(TAG, "Firebase reg id: " + regId);

    if (!TextUtils.isEmpty(regId))
        txtRegId.setText("Firebase Reg Id: " + regId);
    else
        txtRegId.setText("Firebase Reg Id is not received yet!");
 }

@Override
protected void onResume() {
    super.onResume();
   LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.REGISTRATION_COMPLETE));
   LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(Config.PUSH_NOTIFICATION));

    NotificationUtils.clearNotifications(getApplicationContext());
}

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

我将添加火基消息库是:

compile 'com.google.firebase:firebase-messaging:11.0.4'

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    您无需在 BroadcastReceiver 中订阅,只需在 FirebaseInstanceIdService 的 onTokenRefresh 方法中进行即可

    您不需要在 BroadcastReceiver 中获取推送通知,您必须在 FirebaseMessagingService 中的 onMessageReceive 中进行此操作

    FCM 对模拟器极其不可靠,只需使用真实设备,我一直在努力解决这个问题,在某些情况下,我什至在几天后为其他项目打开模拟器时收到通知,用真实手机进行测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多