How to localise the Firebase remote notification body text in Android?

708 views Asked by At

I want to localise the firebase notification sent by the server. Through php I am sending remote notification to Android device. The following postdata I am using in php:

$postData = array(
       'to' => ''.$fcmtoken,
      'priority' => 'high',
       'notification'=> array("body"=> "message body",  "loc_key" => "body_key" "title"=> "message title",                  "icon" => "ic_stat_gdr_notification_icon", 
           "sound" => "default" ),
  );

The notification is being sent on device properly with the body text 'message body'. But I want the localised text ('Cuerpo del menage') of key 'body_key" should be shown.

Also please check the android code which I used to receive the message:

notificationBuilder = new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_stat_gdr_notification_icon)
                            .setContentTitle(from)
                            .setContentText(message)
                            .setSound(defaultSoundUri)
                            .setContentIntent(contentIntent)
                            .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

                }

                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                manager.notify(1, notificationBuilder.build());

Here I don't know that how can I use the key which I got from notification message to localise it? the loc_key is a string. But in android it is expecting 'integer' to be passed in this method: getResources().getString(R.string.body_key) where 'R.string.body_key' is an integer.

So how can I pass the loc_key in getString() method and get localised text? Please help me.

1

There are 1 answers

0
Shriyansh Gautam On BEST ANSWER

You can get the integer id corresponding to your body_key like this

public static int getStringIdentifier(Context context, String name) {
    return getResources().getIdentifier(name, "string", 
        getPackageName());
}


getResources()
 .getString(getStringIdentifier(getApplicationContext(),loc_key));