I implemented the mapping between devices, registration ids, and groups as explained here
Basically came up with a table where to store the device reg id and the notification key of the group to which it is registered (if any) , and added to my user table two fields to keep track of the number of devices that user has logged into and what is his pre determined group name.
Unfortunately, while testing, I incurred into a very strange behavior when refreshing the token of a user, who is also the only member of a device group.
as explained in the comments to the question i asked here
if the previous token was the only member of a device group, after getting refreshed it is invalidated but the group keeps existing and somehow the new token gets added to it. i can even successfully remove the new token from the group thus deleting it, or i can notify the group "waking it up" and making it to auto remove itself
in the couple of tries i have done right now before posting this is what happened:
- get a new token say
reg_id1
- register
reg_id1
to device grouptest_group
(creating it)
code to create group:
curl --header "Authorization: your_key"
--header Content-Type:"application/json"
-H "project_id:your_id" https://android.googleapis.com/gcm/notification
-d "{ \"operation\": \"create\", \"notification_key_name\":
\"test_group\", \"registration_ids\": [\"reg_id1\" ] }"
- test that the group exists
with this
curl -v -H "Content-Type:application/json" -H "Authorization:key=your_key"
-H "project_id:your_id"
https://android.googleapis.com/gcm/notification?notification_key_name=test_group
- refresh
reg_id1
with the way you prefer (deleting app data if on android or requesting new token if on web app ) - check what happened to the group with the command at point 3
the group still exists (even if the only token there is now invalid)
- try deleting old token. what should happen? well, the answer is reasonable..the token we are trying to remove is not valid anymore
code to remove
curl --header "Authorization: key=your_key"
--header Content-Type:"application/json" -H "project_id:your_id"
https://android.googleapis.com/gcm/notification
-d "{ \"operation\": \"remove\", \"notification_key_name\":
\"test_group\", \"notification_key\": \"group_key\",
\"registration_ids\": [\"reg_id1\" ] }"
- Here comes the fun: try deleting the new refreshed token from the same group ! you will get a notification key in return , meaning that there were no errors..but wait ! how could it be?
- ping the group again with command at point 3 and.. the group still exists?! (previously, at the time of the problem in original question, while removing the new token from android, the group would cease to exist after this point..now with curl it continues living)
- try notifying the group then!
code
curl -X POST -H "your_key"
-H "Content-Type: application/json"
-d '{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1",
"icon": "firebase-logo.png",
"click_action": "http://localhost:8081"
},
"to": "group_key"
}' "https://fcm.googleapis.com/fcm/send"
- what happens now is that you get 1 failure (which is ok) and if you try to ping the group again it will be magically disappeared..
Sorry for being so long but i wanted to write all the steps so that you can reproduce the behavior easily.
Can someone explain all of this?
In my android app if the user deletes app data he will need to login again, since the creation/add to a device group is upon login, i need to delete the old token from the group (and delete the group if it was the last) so that he will be able to re-enter the group again (or create it).
But i can't remove the old token directly because it is invalid.
still, the group continues existing even if its only member is invalid. should i just notify it to wake it up
and make it delete itself?
should i remove the new token from the group as well ? (in android this would strangely lead to deleting the group)
EDIT: after testing this morning i have a new use case:
- Create group with reg_id1 subscribed to it
- refresh reg_id1
- recreation of group says that group exists ( i can ping it ad receive a notification key in response)
- try to delete new token from group gives error 500
- notification test to the group gives 0 success and 0 failure
- try to remove old token from group gives back notification key (should be a positive answer)
- now group doesn't exist anymore
EDIT2:
after lots and lots of testing i came to these conclusions:
- refreshing a token on android application invalidates it, if it was the only member of a device group, a simple notification to that group will wake it up and make it auto delete itself.
- refreshing a token on a web application does not invalidate the token, if it was the only member of a device group, notifying the group will result in a 0 success 0 failure. while being invisible the token is still registered to the group and the group will cease to exist only when the old token will be removed. in this case, even if still there the old token won't concur to the limit of 20 devices ( i successfully registered and refreshed 25 tokens to the same group). What is needed now is a way to retrieve the old token to remove it from the group , in a web application scenario. this is crucial since a web application doesn't have a unique id therefore it can't be mapped as in android (with the android id) to the registration token, and the old token won't even appear as a failure when notifying, meaning that it's almost impossible to retrieve it after it was refreshed.