关于FCM本地多语言处理

关于FCM可发送的消息类型

  • 通知消息,有时被称为“显示消息”。此类消息由 FCM SDK 自动处理。
  • 数据消息,由客户端应用处理。

因为需求方是使用 Firebase 控制台发送通知消息,并且不管是应用是未启动还是在后台,我们都能收到消息并做多语言处理。所以要做本地多语言就需要我们拦截FCM的消息,然后自己重新处理。

1.lz用的FCM版本

implementation 'com.google.firebase:firebase-messaging:23.2.0'

2.重写handleIntent

    @Override
    public void handleIntent(Intent intent) {
        Log.d("h=====FCM","handleIntent");
        Bundle bundle = intent.getExtras();
        //自定义参数
        String bodyKey = "body_key";
        String vv = intent.getStringExtra(bodyKey);
        if (bundle != null && vv !=null) {
            String notificationBody = "";
            //获取本地化多语言
            if(vv.equals("109613")){
                notificationBody = getString(R.string.a109613);
            }
            //替换通知文字
            String bodyName = "gcm.notification.body";
            intent.putExtra(bodyName,notificationBody);
        }
        //firebase通知处理
        super.handleIntent(intent);
    }

3.创建多语言文件

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="a109613" translatable="false">six six six</string>
</resources>

猜你喜欢

转载自blog.csdn.net/qq_40150532/article/details/131803625