0

我想本地化服务器发送的Firebase通知。通过PHP我发送远程通知到Android设备。下面POSTDATA我使用的PHP:如何在Android中本地化Firebase远程通知正文文本?

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

通知正在对设备与正文“消息体”发送正常。但我想本地化文本(“Cuerpo德尔家政”)键“body_key”应该显示的

还请我用来接收消息的Android代码:

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());

这里我不知道如何使用从通知消息中获得的密钥来定位它?loc_key是一个字符串,但是在android中,它期望在此方法中传递'integer':getResources( ).getString(R.string.body_key)其中'R.string.body_key'是一个整数。

那么我怎样才能通过getString()方法loc_key并获得本地化的文本?请帮帮我。

回答

0

你可以得到一个整数id对应的body_key这样

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


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

非常感谢。它现在有效! –