Android利用极光推送获取到消息并缓存至本地

来源:互联网 发布:陕西师大远程网络教育 编辑:程序博客网 时间:2024/06/01 07:13

缓存的方式有多种,最常用的类似搜索记录,这些用的数据库比较多。
本文用的是一个数据库框架GreenDao,正好也练习一下。

关于技术部分需要的操作也不是太多,无非包括两部分:
一部分是在接到推送的消息的时候缓存,另一部分是在页面的时候将消息展示出来。

但是有个缺点,数据清除了之后,除非自己去后台查看记录,不然就被清理掉了。

源码在GitHub如果有介绍不清楚的地方以去查看
https://github.com/wapchief/android-CollectionDemo

关于GreenDao的介绍这里就不详细描述了。
直接开始代码部分。

1、需要集成GreenDao,和极光JPush两个sdk。

Modle App:

apply plugin: 'org.greenrobot.greendao'......greendao {    schemaVersion 1//数据库版本号    daoPackage 'wapchief.com.collectiondemo.greendao'//设置DaoMaster、DaoSession、Dao包名    targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录    //targetGenDirTest:设置生成单元测试目录    //generateTests:设置自动生成单元测试用例}......dependencies {    compile 'cn.jiguang.sdk:jpush:3.0.3'  // 此处以JPush 3.0.3 版本为例。    compile 'cn.jiguang.sdk:jcore:1.1.1'  // 此处以JCore 1.1.1 版本为例。    compile 'org.greenrobot:greendao:3.2.0'}

Project:

    dependencies {        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'        // in the individual module build.gradle files    }

建议参考:
Android SDK 集成指南
GreenDao官方文档

在集成GreenDao的时候有可能被墙,导致下载不下来,可以修改代理为127.0.0.1

2、创建实体类用于生成数据库
/** * Created by Wu on 2017/5/11 0011 上午 9:24. * 描述:存放极光推送的消息 */@Entitypublic class Message {    @Id(autoincrement = true)    private Long id;    private String title;    private String content;    @Override    public String toString() {        return "Message{" +                "id=" + id +                ", title='" + title + '\'' +                ", content='" + content + '\'' +                '}';    }    @Generated(hash = 977969778)    public Message(Long id, String title, String content) {        this.id = id;        this.title = title;        this.content = content;    }    @Generated(hash = 637306882)    public Message() {    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public String getContent() {        return content;    }    public void setContent(String content) {        this.content = content;    }}

创建之后,运行Build > Make Module app,会生成数据库操作的DAO类。
如果实体是Message,那么自动生成的就是MessageDao.

3、初始化数据库相关
    private void initDbHelp() {        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(BaseApplication.mBaseApplication, "recluse-db", null);        SQLiteDatabase db = helper.getWritableDatabase();        DaoMaster daoMaster = new DaoMaster(db);        DaoSession daoSession = daoMaster.newSession();        messageDao = daoSession.getMessageDao();    }
4、在广播器里操作

在集成JPush的时候需要在本地建立本地广播继承BroadcastReceiver,一般命名为MyReceiver

public class MyReceiver extends BroadcastReceiver{    private static final String TAG = "JPush";    MessageDao messageDao;    @Override    public void onReceive(Context context, Intent intent) {        Bundle bundle = intent.getExtras();        //初始化数据库        initDbHelp();.......        else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知:"+bundle.getString(JPushInterface.EXTRA_ALERT));            String content = bundle.getString(JPushInterface.EXTRA_ALERT);            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);            SimpleDateFormat formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");            Date curDate =  new Date(System.currentTimeMillis());            //获取当前时间            String   str   =   formatter.format(curDate);            messageDao.insert(new Message(null, str, content));            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {            Log.d(TAG, "[MyReceiver] 用户点击打开了通知");            //打开自定义的Activity            Intent i = new Intent(context, MessageActivity.class);            i.putExtras(bundle);            //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );            context.startActivity(i);        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {            Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..        } else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);            Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);        } else {            Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());        }    }

我们所收到的推送就在这里进行处理。
通过JPushInterface.EXTRA_ALERT获取系统推送的消息。
然后使用
messageDao.insert(new Message(null, str, content));
将消息存放到数据库,为了区分,这里加了一个推送的时间。
这时候数据库已经存在了该条消息。

还有个点击通知的操作,一般是跳转到消息列表或者详情。
直接在里面写事件即可。

5、在Activity里展示消息
    private void initview() {        //查询所有        list = messageDao.queryBuilder().list();        //list倒序排列        Collections.reverse(list);        adapter = new ItemTVAdapter(context, list);        messageLv.setAdapter(adapter);        adapter.notifyDataSetChanged();    }

这里只需要一个简单的查询全部的语句就可以。

效果图

0 0