繁体   English   中英

未调用FCM onMessageReceiver

[英]FCM onMessageReceiver not getting called

我正在为聋人+盲人创建一个应用程序(他们可以在屏幕上看到颜色,但是看不到细节)。 我想振动智能手表并在有人敲门铃时显示某种颜色。 门铃将通过节点通过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);
      });
  });
}

Smartwatch通过以下服务接收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并且不会打开活动。 为什么服务不再接收消息? 以及我如何解决它,以便该服务将始终收到该消息。 消息始终需要尽快传递给用户,因为它被用作聋哑人的门铃。

如果您不希望延迟,则必须将此延迟添加到您的有效负载priority : 'high' 但请记住,这会增加设备的电池使用量等。

请访问此页面以获取更多信息。

经过一些测试,我得到它的工作。 我使用的npm模块似乎有问题。 我使用了Firebase管理员 ,该管理员已获得了我的Firebase文档。 除上面示例发送消息不会触发后台服务外,它工作正常。 为了使它正常工作,我遵循了以下步骤

为了通过节点触发应用程序在后台触发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);
      });
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM