【问题标题】:How to save Push Notification Data in SharedPreferences and display in RecyclerView如何在 SharedPreferences 中保存推送通知数据并在 RecyclerView 中显示
【发布时间】:2016-04-15 03:27:41
【问题描述】:

我创建了一个从 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"));
        }
    }
}

【问题讨论】:

  • 那么你在哪一部分遇到了问题?
  • 无法将字符串保存在 sharedpreferences 中,也在我填充 RecyclerView 的片段中我无法访问 sharedpreferences 文件
  • 在下面添加了我的答案。看看吧。

标签: android push-notification broadcastreceiver sharedpreferences


【解决方案1】:

创建一个自定义 PushData 类并使用 Gson 库将您的数据保存为字符串。

这是一个干净的example

【讨论】:

  • 我的数据是纯文本
【解决方案2】:

试试看。

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

【讨论】:

    【解决方案3】:

    用于将字符串放入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");
    

    【讨论】:

      【解决方案4】:

      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", "");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多