【问题标题】:FCM onMessageReceiver not getting calledFCM onMessageReceived 没有被调用
【发布时间】:2018-12-03 19:18:39
【问题描述】:

我正在为聋人和盲人创建一个应用程序(他们可以在屏幕上看到颜色,但看不到细节)。每当有人按门铃时,我想振动智能手表并显示某种颜色。门铃将通过节点通过Firebase通过节点向用户发送消息,请参见下面的示例:

import admin from 'firebase-admin';

// tslint:disable-next-line:no-var-requires
const serviceAccount = require('../../../firebase.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://example.firebaseio.com',
});

export function sendMessageToUser(
  token: string,
  payload: { data: { color: string; vibration: string; text: string } },
  priority: string,
) {
  const options = {
    priority,
    timeToLive: 60 * 60 * 24,
  };

  return new Promise((resolve, reject) => {
    admin
      .messaging()
      .sendToDevice(token, payload, options)
      .then(response => {
        console.log(response);
        resolve(response);
      })
      .catch(error => {
        console.log('error', error);
        reject(error);
      });
  });
}

智能手表通过以下服务接收firebase消息:

public class HapticsFirebaseMessagingService extends FirebaseMessagingService {

    private SharedPreferences sharedPreferences;

    @Override
    public void onCreate() {
        super.onCreate();

        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    public void onNewToken(String token) {
        super.onNewToken(token);

        sharedPreferences.edit().putString("fb", token).apply();
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Map<String, String> data = remoteMessage.getData();
        String color = data.get("color");
        String vibration = data.get("vibration");
        String text = data.get("text");

        Intent dialogIntent = new Intent(this, AlarmActivity.class);
        dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle bundle = new Bundle();
        bundle.putString("color", color);
        bundle.putString("vibration", vibration);
        bundle.putString("text", text);
        dialogIntent.putExtras(bundle);
        startActivity(dialogIntent);
    }

    /**
     * Get the token from the shared preferences.
     */
    public static String getToken(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getString("fb", "empty");
    }
}

当智能手表连接到计算机时,这可以正常工作,但是当我将智能手表与计算机断开连接时,它可以工作几分钟。但是几分钟后onMessageReceived 没有被调用,也不会打开活动。为什么 de service 不再接收消息?以及如何修复它,以便服务始终收到消息。消息总是需要尽可能快地传递给用户,因为它被用作聋人和盲人的门铃。

【问题讨论】:

    标签: android node.js firebase firebase-cloud-messaging android-service


    【解决方案1】:

    如果您不想延迟,则必须将其添加到您的有效负载 priority : 'high'。但请记住,这会在您的设备中消耗更多的电池电量等。

    请访问page了解更多信息。

    【讨论】:

    • 我已经在有效载荷中添加了优先级:'high'。是sendMessageToUser中的参数优先级。
    • 是的,我看到了参数,但不能确定它是高还是正常。
    【解决方案2】:

    经过一些测试,我得到了它的工作。我使用的 npm 模块似乎有问题。我使用了 Firebase admin,这是我的 firebase 文档。除了使用上面的示例发送消息不会触发后台服务之外,它工作正常。为了使它正常工作,我遵循了这些steps

    为了在应用程序通过节点在后台时触发 onmessageReceived,我使用了以下脚本:

    function sendMessageToUser(
      token: string,
      data: { color: string; vibration: string; text: string },
      priority: string,
    ) {
      return new Promise((resolve, reject) => {
        fetch('https://fcm.googleapis.com/fcm/send', {
          method: 'POST',
          body: JSON.stringify({
            data,
            priority,
            to: token,
          }),
          headers: {
            'Content-type': 'application/json',
            Authorization: `key=${process.env.FIREBASE_API_KEY}`,
          },
        })
          .then(async (response: any) => {
            resolve(response);
          })
          .catch((exception: any) => {
            reject(exception);
          });
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-29
      • 1970-01-01
      • 2018-04-12
      • 2021-04-26
      • 1970-01-01
      • 2018-07-10
      • 2018-10-01
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多