我创建了一个从PushBots接收推送通知的应用程序。
我已成功接收推送通知,但我想将推送数据存储在SharedPreferences中,并显示另一个包含RecyclerView的活动。
我知道内容提供商是一种更好的方法,但我现在想坚持使用SharedPreferences。
这是我的自定义广播接收器
public class customHandler extends BroadcastReceiver
{
private static final String TAG = "customHandler";
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, "action=" + action);
// Handle Push Message when opened
if (action.equals(PBConstants.EVENT_MSG_OPEN)) {
//Check for Pushbots Instance
Pushbots pushInstance = Pushbots.sharedInstance();
if(!pushInstance.isInitialized()){
Log.d("Initializing Pushbots.");
Pushbots.sharedInstance().init(context.getApplicationContext());
}
//Clear Notification array
if(PBNotificationIntent.notificationsArray != null){
PBNotificationIntent.notificationsArray = null;
}
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_OPEN);
Log.w(TAG, "User clicked notification with Message: " + PushdataOpen.get("message"));
//Report Opened Push Notification to Pushbots
if(Pushbots.sharedInstance().isAnalyticsEnabled()){
Pushbots.sharedInstance().reportPushOpened( (String) PushdataOpen.get("PUSHANALYTICS"));
}
//Start Main Activity On CLicking Notification
String packageName = context.getPackageName();
Intent resultIntent = new Intent(context.getPackageManager().getLaunchIntentForPackage(packageName));
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.putExtras(intent.getBundleExtra("pushData"));
Pushbots.sharedInstance().startActivity(resultIntent);
// Handle Push Message when received
}else if(action.equals(PBConstants.EVENT_MSG_RECEIVE)){
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_RECEIVE);
Log.w(TAG, "User Received notification with Message: " + PushdataOpen.get("message"));
}
}
}
答案 0 :(得分:0)
创建一个自定义PushData类,并使用Gson库将数据保存为字符串。
这是一个干净的example
答案 1 :(得分:0)
试一试。
public void addNotification(String notification) {
// get old notifications
String oldNotifications = getNotifications();
if (oldNotifications != null) {
oldNotifications += "|" + notification;
} else {
oldNotifications = notification;
}
editor.putString(KEY_NOTIFICATIONS, oldNotifications);
editor.commit();
}
public String getNotifications() {
return pref.getString(KEY_NOTIFICATIONS, null);
}
得到它
String oldNotification = AppController.getInstance().getPrefManager().getNotifications();
List<String> messages = Arrays.asList(oldNotification.split("\\|"));
答案 2 :(得分:0)
将字符串放入SharedPreferences
:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
editor.commit();
获取片段中的共享首选项:
SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString(key, "default value");
答案 3 :(得分:0)
1。在onReceive
方法中初始化共享首选项变量,如下所示
SharedPreferences prf = context.getSharedPreferences("YOUR_PREFERENCE_NAME", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prf.edit();
2。收到时将推送消息保存到共享首选项
................
................
// Handle Push Message when received
}else if(action.equals(PBConstants.EVENT_MSG_RECEIVE))
{
HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_RECEIVE);
Log.w(TAG, "User Received notification with Message: " + PushdataOpen.get("message"));
String message=(String) PushdataOpen.get("message");
editor.putString("push_message", message);
editor.commit();
}
3. 从共享首选项中获取已保存的推送消息以进行显示,
SharedPreferences prf = context.getSharedPreferences("YOUR_PREFERENCE_NAME", Context.MODE_PRIVATE);
String message=prf.getString("push_message", "");