简体   繁体   中英

Android push notification with sound in one language

Hello. I have an android app in two language (es and fr) . I´m using push notifications and I would like to reproduce a sound depending of the language that the user select on my app screen settings. The problem is when the push come and the app is closed so I think I can´t access to the user language.

I mean, an example: User opens the app and selects Spanish language between fr and es ( I save it in shared-preferences) . User close the app. Two hours later user receives a push notification, so in that moment I want to reproduce a Spanish sound but note that the app is not running in that moment.

That is possible? . Can I access to shared-preferences and get the language ?

Thanks in advance.

I do not know how you are receiving these notifications that show up after two hours. However, if it were me, I would use an alarm manager to call a service which would check the language preference set and send the notification in the appropriate language.

Since your question is particular on accessing a shared a preference, this could as well be done in the Service as shown below:

//getting the shared preference...

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//this -> Service class as service is inherited from a context
String lang = sharedPref.getString(Settings.KEY_LANGUAGE_PREFS,
    getResources().getString(R.string.language_default_value));
//provide default value

If I could add, I am not sure if these notifications are coming from the app itself or the web server. Either way it is up to you to specify both languages. If it is within the app, then be sure to have string.xml values in both value language folders. Check out the developer notes for details.

解决了,我在调用shared-preferences方法时出错,所以回答了我自己的问题:是的,当应用未运行时,我们可以访问共享首选项并通过推送通知获取参数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM