1

I am creating a application for Deaf + Blind people (They can see colors on the screen, but can't see details). I want vibrate the smartwatch and show a certain color whenever someone rings the doorbell. The doorbell will send through node a message to a user through Firebase via node, see example below:

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);
      });
  });
}

And the smartwatch receives the firebase message through the following service:

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");
    }
}

This works fine when the smartwatch is connected to the computer, but when i disconnect the smartwatch from the computer it works for a few minutes. But after some minutes onMessageReceived is not called and will not open the activity. Why does de service no longer receives messages? And how do i fix it so the service will always receive the message. The messages always needs to be delivered as fast as possible to the users, because it is used as a doorbell for deaf+blind people.

Nylsoo
  • 159
  • 1
  • 2
  • 11

2 Answers2

0

If you want no delay, you have to add this to your payload priority : 'high'. But keep it in mind, this costs more battery usage etc. in your device.

Please visit this page for more informations.

Murat Güç
  • 377
  • 4
  • 9
0

After some testing i got it working. It seemed to be a problem with the npm module i was using. I used Firebase admin which is given my the firebase documentation. It worked fine except that sending a message with the example above doesn't trigger the background service. In order to get this working properly i followed these steps.

To trigger the onmessageReceived when the app is in the background via node i used the following script:

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);
      });
  });
}
Nylsoo
  • 159
  • 1
  • 2
  • 11