My Android app can be used with the google accounts of the device. So if I have account "a.gmail" and account "b.gmail", I can switch between them without having to restart the app.
When I switch to an account, say "a.gmail", I send the GCM regId to the server and can get push messages from it. All fine.
But if a user sends a push message to account "b.gmail" while I'm running the app with account "a.gmail", I loose it. If I switch to account "b.gmail" after the server has sent the message, I don't get any notification.
Is there a way to handle a scenario like this ? I thought that the flag delay_while_idle
would have been enough, but it doesn't help.
This is how I format the message (PHP):
$fields = array(
'to' => $to,
'delay_while_idle' => true,
'data' => array(
"title" => "this is the title",
"body" => "this is the body",
"icon" => "@drawable/icon_notif")
);
As per the latest GCM server setup recommendation, you are going to install your own server which is responsible for pushing the message to the GCM. Your device mostly do communication to your server to set registration ID and other local changes.
If your server is designed in such a way the (user sends a push message to account "b.gmail") is happening via the server. Then..
You have to tell your server that you have switched the account, indirectly that the user message should not sent to you yet.
I've found a solution, maybe it could be useful for someone with a similar problem so I write it down.
Luckily I'm storing the user in the database using an "id" but also using the SIM and SubscriberId of the device. So, for each device, I get all the registration ids of the user if he has multiple accounts.
The query is something like this:
SELECT `RegId` FROM `UserGCM` WHERE `UserId` IN
(
SELECT ru2.`UserId`
FROM `RegisteredUser` ru1
INNER JOIN `RegisteredUser` ru2 ON
ru1.`SubscriberId` = ru2.`SubscriberId` AND ru1.`SIM` = ru2.`SIM`
WHERE ru1.`UserId` = ?
)
Then for each regId found, I send a message:
...
$query->bind_param('i', $did);
$query->execute();
$query->bind_result($regId);
while ($row = $query->fetch())
{
$response = $gcpm->send($regId, $did, $nam);
}
did
is the destination id that is needed by the client to know who was the recipient.
The client will still get only one message but it knows now if the message is for the current account or for another one. So now, if the user clicks the message, I can ask if he wants to switch to the other account to see the message.