Android利用極光推送獲取到消息并緩存至本地

緩存的方式有多種,最常用的類(lèi)似搜索記錄,這些用的數(shù)據(jù)庫(kù)比較多。
本文用的是一個(gè)數(shù)據(jù)庫(kù)框架GreenDao,正好也練習(xí)一下。

關(guān)于技術(shù)部分需要的操作也不是太多,無(wú)非包括兩部分:
一部分是在接到推送的消息的時(shí)候緩存,另一部分是在頁(yè)面的時(shí)候?qū)⑾⒄故境鰜?lái)。

但是有個(gè)缺點(diǎn),數(shù)據(jù)清除了之后,除非自己去后臺(tái)查看記錄,不然就被清理掉了。

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

關(guān)于GreenDao的介紹這里就不詳細(xì)描述了。
直接開(kāi)始代碼部分。

1、需要集成GreenDao,和極光JPush兩個(gè)sdk。

Modle App:

apply plugin: 'org.greenrobot.greendao'
......
greendao {
    schemaVersion 1//數(shù)據(jù)庫(kù)版本號(hào)
    daoPackage 'wapchief.com.collectiondemo.greendao'//設(shè)置DaoMaster、DaoSession、Dao包名
    targetGenDir 'src/main/java'//設(shè)置DaoMaster、DaoSession、Dao目錄
    //targetGenDirTest:設(shè)置生成單元測(cè)試目錄
    //generateTests:設(shè)置自動(dòng)生成單元測(cè)試用例
}
......
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的時(shí)候有可能被墻,導(dǎo)致下載不下來(lái),可以修改代理為127.0.0.1

2、創(chuàng)建實(shí)體類(lèi)用于生成數(shù)據(jù)庫(kù)
/**
 * Created by Wu on 2017/5/11 0011 上午 9:24.
 * 描述:存放極光推送的消息
 */
@Entity
public 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;
    }
}

創(chuàng)建之后,運(yùn)行Build > Make Module app,會(huì)生成數(shù)據(jù)庫(kù)操作的DAO類(lèi)。
如果實(shí)體是Message,那么自動(dòng)生成的就是MessageDao.

3、初始化數(shù)據(jù)庫(kù)相關(guān)
    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的時(shí)候需要在本地建立本地廣播繼承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();
        //初始化數(shù)據(jù)庫(kù)
        initDbHelp();
        
.......

        else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下來(lái)的通知:"+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());
            //獲取當(dāng)前時(shí)間
            String   str   =   formatter.format(curDate);
            messageDao.insert(new Message(null, str, content));

            Log.d(TAG, "[MyReceiver] 接收到推送下來(lái)的通知的ID: " + notifactionId);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 用戶(hù)點(diǎn)擊打開(kāi)了通知");

            //打開(kāi)自定義的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] 用戶(hù)收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在這里根據(jù) JPushInterface.EXTRA_EXTRA 的內(nèi)容處理代碼,比如打開(kāi)新的Activity, 打開(kāi)一個(gè)網(wǎng)頁(yè)等..

        } 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());
        }
    }

我們所收到的推送就在這里進(jìn)行處理。
通過(guò)JPushInterface.EXTRA_ALERT獲取系統(tǒng)推送的消息。
然后使用
messageDao.insert(new Message(null, str, content));
將消息存放到數(shù)據(jù)庫(kù),為了區(qū)分,這里加了一個(gè)推送的時(shí)間。
這時(shí)候數(shù)據(jù)庫(kù)已經(jīng)存在了該條消息。

還有個(gè)點(diǎn)擊通知的操作,一般是跳轉(zhuǎn)到消息列表或者詳情。
直接在里面寫(xiě)事件即可。

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

    }

這里只需要一個(gè)簡(jiǎn)單的查詢(xún)?nèi)康恼Z(yǔ)句就可以。

效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 推送技術(shù)產(chǎn)生場(chǎng)景: --服務(wù)器端主動(dòng)性: 客戶(hù)端與服務(wù)器交互都是客戶(hù)端主動(dòng)的, 服務(wù)器一般不能主動(dòng)與客戶(hù)端進(jìn)行數(shù)據(jù)...
    原軍鋒閱讀 35,204評(píng)論 4 60
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,008評(píng)論 25 709
  • 版本記錄 前言 ??現(xiàn)在很多APP都有推送功能,其中極光推送就是很多APP的首選。我們最近的幾個(gè)APP也是用的極光...
    刀客傳奇閱讀 8,672評(píng)論 0 8
  • 不同版本極光推送SDK集成各有差異,集成時(shí)一定要注意版本號(hào),樓主已將博文更新成最新的SDK JPush v3.0....
    i順頌時(shí)宜閱讀 8,016評(píng)論 37 170
  • 帶娃神器有很多,最近解救我于水火之中的是背帶。 為了一家團(tuán)聚,放暑假后候鳥(niǎo)似的從老家到了深圳。...
    嗨Pi麻閱讀 302評(píng)論 0 0

友情鏈接更多精彩內(nèi)容