在app上本地存储推送通知

时间:2017-05-29 05:46:58

标签: java android push-notification sharedpreferences

目前,我能够显示,推送通知并能够在Recycler View上显示。但是,每当我关闭应用程序并重新打开时,推送通知都将消失。

我读到使用SharedPreferences会保存,但它不会保存我的通知,我真的不知道如何将其实现到我的代码中。我希望在打开应用程序时显示通知。

我刚刚遇到的另一个问题是,我发送的每个新推送通知都会替换旧的通知。例如,如果我发送通知消息“test”和第二个通知消息“test2”,test2将覆盖测试。我能够成功显示所有通知,但在实现SharedPreferences后,我得到了覆盖的值。

enter image description here

NotificationFragment.java

public class NotificationFragment extends Fragment {
    private TextView test;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<Notification> notificationList;
    private BroadcastReceiver activityReceiver;
    private String MyPREFERENCES = "test";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_notification, container, false);


        mRecyclerView = (RecyclerView) v.findViewById(R.id.notificationRecyclerview);
        notificationList = new ArrayList<>();
        mAdapter = new NotificationAdapter(getContext(), notificationList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        activityReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Bundle bundle = intent.getBundleExtra("msg");
                String body = bundle.getString("msgBody");
                SharedPreferences sharedpreferences = getContext().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("notification", body);
                editor.apply();
                Log.d("body", body);
                Notification obj = new Notification(body);
                prepareNotification(obj.getBody());

            }

        };

        if (activityReceiver != null) {
            IntentFilter intentFilter = new  IntentFilter("ACTION_STRING_ACTIVITY");
            getActivity().registerReceiver(activityReceiver, intentFilter);
        }

        return v;
    }

    private void prepareNotification(String body){
        Notification obj = new Notification(body);
        notificationList.add(obj);
        mAdapter = new NotificationAdapter(getContext(), notificationList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }


}

NotificationAdapter.java

public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.NotificationHolder> {
    private List<Notification> notificationList;
    private Context mContext;
    private String MyPREFERENCES = "test";
    public SharedPreferences prefs;


    public NotificationAdapter(Context mContext, List<Notification> notificationList) {
        this.mContext = mContext;
        this.notificationList = notificationList;
        prefs = mContext.getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
    }


    @Override
    public NotificationHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflatedView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_notification, parent, false);
        return new NotificationHolder(inflatedView);
    }

    @Override
    public void onBindViewHolder(final NotificationHolder holder, int position) {
        Notification notification = notificationList.get(position);
//        String restoredText = prefs.getString("text", null);
//        holder.body.setText("Notification Message" + notification.getBody());
        holder.body.setText("Notification Message: " + prefs.getString("notification", "No notifcation"));

    }

    @Override
    public int getItemCount() {
        return notificationList.size();
    }

    public class NotificationHolder extends RecyclerView.ViewHolder {
        public TextView body;

        public NotificationHolder(View view) {
            super(view);
            body = (TextView) view.findViewById(R.id.bodyMsg);
        }
    }
}

0 个答案:

没有答案
相关问题