rdelrosario / xamarin-plugins

Cross platform Xamarin & Windows plugins for PCLs
MIT License
179 stars 161 forks source link

Android: body_loc_key only working when app in background #90

Open CR4567 opened 7 years ago

CR4567 commented 7 years ago

Hi,

sometimes android has really a weird behavior.... :-) I'm using the body_loc_key for translation of my push notifications (as described here https://developers.google.com/cloud-messaging/http-server-ref). The weird thing is that everything works as expected, if message arrives while app is in background. If app is open the normal body variable is used. Or if I send a blank message only with the body_loc_key the message is completely empty. Is the message treated different when open? I thought the only difference is that if it's open the OnMessage function is triggered, but the shouldn't be a problem. Right?

The translation files are available in the resource folder and as I said, working when in background.

CR4567 commented 7 years ago

Since I couldn't find a solution for the problem, I wrote a simple code snippet for the PushNotificationGCMListener:

int identifier = Resources.GetIdentifier(message, "string", PackageName);

                if (identifier != 0)
                {
                    if (parameters.ContainsKey(PushNotificationKey.Args))
                    {
                        var args = parameters[PushNotificationKey.Args];
                        message = Resources.GetString(identifier, (Java.Lang.Object)args);
                    }
                    else { 
                        message = Resources.GetString(identifier);
                    }
                }

It checks wether there is a Resource with the given name and than checks for arguments. At the end overrides the original message with the translated message from the resource file.

Does that make sense to you? If so, is it possible to integrate into your branch?