mobizt / Firebase-ESP32

[DEPRECATED]🔥 Firebase RTDB Arduino Library for ESP32. The complete, fast, secured and reliable Firebase Arduino client library that supports CRUD (create, read, update, delete) and Stream operations.
MIT License
415 stars 118 forks source link

HELP #61

Closed blacxcode closed 4 years ago

blacxcode commented 4 years ago

How to implement remove device token after sending a message? I used this code but not send any more for the next user. I've got the error message "error":"InvalidRegistration"

define FIREBASE_FCM_SERVER_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

define MAX_USER 100

define MAX_DEVICE 10

FirebaseData firebaseFCM;

String mUser[MAX_USER]; String mTokenDevice[MAX_DEVICE]; String mTitle = "This is title"; String mBody = "This is body" String mDataMessage = "{" "\"data\" : {" "\"title\" : \"" + mTitle + "\"," "\"body\" : \"" + mBody + "\"" "} }"

for (int z = 0; z < MAX_USER; z++) { if (mUser[z] == "") break;

 firebaseFCM.fcm.begin(FIREBASE_FCM_SERVER_KEY);
 firebaseFCM.fcm.setPriority("high");
 firebaseFCM.fcm.setTimeToLive(5000);
 firebaseFCM.fcm.setDataMessage(mDataMessage);

 int index = 0;
 for (int y= 0; y< TOTAL_DEVICE; y++) {
      if (mTokenDevice[y] == "") break;

      firebaseFCM.fcm.addDeviceToken(mTokenDevice[y]);
      index = y;
      mTokenDevice[y] = "";
 }
 Firebase.sendMessage(firebaseFCM, index);
 firebaseFCM.fcm.removeDeviceToken(index);

}

mobizt commented 4 years ago

If firebaseFCM is FirebaseData object.

To clear all device tokens. firebaseFCM.fcm.clearDeviceToken();

To get the sending error. firebaseFCM.errorReason();

To send a message to a recipient, you need to provide the index of device token which should not greater than the number of device tokens you 've added minus one otherwise the error will occur.

Add and remove device tokens are just adding and removing data from a dynamic array of string. It is simple as you use std::vector<String> in C++.

And make sure you send messages to existent users or devices (tokens).

blacxcode commented 4 years ago

I've checked, the problem is that token sent from the device is invalid. Thanks, Bro