Android uses Aurora push to get messages and cache them locally

Keywords: Database SDK github Android

There are many ways of caching, the most commonly used is similar to search records, which use more databases.
GreenDao, a database framework, is used in this article, just to practice it.

There are not too many operations needed for the technical part, but there are only two parts:
One part is to cache when a push message is received, the other part is to display the message on the page.

But there is a disadvantage. When the data is cleared, it will be cleared up unless you go to the background to check the records.

Source code in GitHub if there is not a clear introduction to the place to see
https://github.com/wapchief/android-CollectionDemo

The introduction to Green Dao is not described in detail here.
Start the code section directly.

1. It is necessary to integrate Green Dao and Aurora JPush.

Modle App:

apply plugin: 'org.greenrobot.greendao'
......
greendao {
    schemaVersion 1//Database Version Number
    daoPackage 'wapchief.com.collectiondemo.greendao'//Setting DaoMaster, DaoSession, Dao package name
    targetGenDir 'src/main/java'//Set up DaoMaster, DaoSession, Dao directories
    //targetGenDirTest: Set up the generated unit test directory
    //GeneeTests: Setting up automatic generation of unit test cases
}
......
dependencies {
    compile 'cn.jiguang.sdk:jpush:3.0.3'  // Take JPush version 3.0.3 as an example.
    compile 'cn.jiguang.sdk:jcore:1.1.1'  // Take JCore Version 1.1.1 as an example.
    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
    }

Suggestions for reference:
Android SDK Integration Guide
Green Dao Official Documents

When integrating GreenDao, it may be walled off, causing downloads to fail. The agent can be modified to 127.0.0.1.

2. Create entity classes to generate databases
/**
 * Created by Wu on 2017/5/11 0011 9:24 a.m.
 * Description: Store Aurora push messages
 */
@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;
    }
}

After creation, running Build > Make Module app generates DAO classes for database operations.
If the entity is Message, the message Dao is automatically generated.

3. Initialization database correlation
    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. Operating in Broadcasters

When integrating JPush, you need to build a local broadcast inheritance Broadcast Receiver, commonly named 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();
        //Initialize the database
        initDbHelp();

.......


        else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] Receive a push notification:"+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 year MM month dd day   HH:mm:ss");
            Date curDate =  new Date(System.currentTimeMillis());
            //Get the current time
            String   str   =   formatter.format(curDate);
            messageDao.insert(new Message(null, str, content));


            Log.d(TAG, "[MyReceiver] Receiving a push notification ID: " + notifactionId);


        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] The user clicks to open the notification");


            //Open custom Activeness
            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] Users receive RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //Here, according to the content processing code of JPushInterface.EXTRA_EXTRA, such as opening a new Activity, opening a web page, etc..


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

The push we receive is processed here.
Get the system push message through JPushInterface.EXTRA_ALERT.
Then use
messageDao.insert(new Message(null, str, content));
The message is stored in the database, and a push time is added to distinguish it.
This message already exists in the database.

There is also a click notification operation, usually jumping to the message list or details.
Write events directly in it.

5. Presenting news in Activity
    private void initview() {
        //Query all
        list = messageDao.queryBuilder().list();
        //Inverse list Arrangement
        Collections.reverse(list);
        adapter = new ItemTVAdapter(context, list);
        messageLv.setAdapter(adapter);
        adapter.notifyDataSetChanged();


    }

All you need here is a simple query of all the statements.

Posted by crazytigger on Mon, 01 Jul 2019 10:53:30 -0700