lundi 5 octobre 2015

Store push messages from GCM into database

I need to get messages from GCM and then store them in database. Actually the main point is to get message and then change a Recycler view in fragment. So as this notifications are coming, they appear in the fragment (so service works in background and saves the messages). I set up a gcmlistener service which have onMessageReceived method and database helper class. But this database helper needs a context, so how can pass a context from GCMlistenerService?

public class MyGcmListenerService extends GcmListenerService {

private static final String TAG = "MyGcmListenerService";
mySQLAdapter adapter = new mySQLAdapter(/*What to put here*/);


@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("price");

    Log.d(TAG, "From: " + from);
    Log.d(TAG, "Message: " + message);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {
        // normal downstream message.
    }      
    sendNotification(message);
    String time = DateFormat.getTimeInstance().toString();
    long id =  adapter.insertData("Information",message,time);
    if (id<0)
    Log.d("GCM","Unsuccess");
    else
    Log.d("GCM","Success");

    Intent intent = new Intent("token");
    intent.putExtra("message",message);
    intent.putExtra("typeOf","Info");
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

}

Blockquote

public class mySQLAdapter { mySQLHelper helper;

public mySQLAdapter(Context context) {
    helper = mySQLHelper.getmInstance(context);
}

public long insertData(String type, String description, String time) {
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(helper.TYPE, type);
    contentValues.put(helper.DESCRIPTION, description);
    contentValues.put(helper.TIME, time);
    long id = db.insert(mySQLHelper.TABLE_NAME, null, contentValues);
    return id;
}

public List<Notification> getData() {
    List<Notification> notifications = new ArrayList<>();
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {mySQLHelper.UID, mySQLHelper.TYPE, mySQLHelper.DESCRIPTION, mySQLHelper.TIME};
    Cursor cursor = db.query(mySQLHelper.TABLE_NAME, columns, null, null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        String type = cursor.getString(cursor.getColumnIndex(mySQLHelper.TYPE));
        String desc = cursor.getString(cursor.getColumnIndex(mySQLHelper.DESCRIPTION));
        String time = cursor.getString(cursor.getColumnIndex(mySQLHelper.TIME));
        Notification notification = new Notification(type, desc, time, 0);
        notifications.add(notification);
    }
    return notifications;
}

public static class mySQLHelper extends SQLiteOpenHelper {
    private static mySQLHelper mInstance = null;
    private static final String DATABASE_NAME = "notificationsDB";
    private static final String TABLE_NAME = "notifications";
    private static final String UID = "_id";
    private static final int VERSION = 0;
    private static final String TYPE = "Type";
    private static final String DESCRIPTION = "Description";
    private static final String TIME = "Time";
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
            + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TYPE + " VARCHAR(255), " + DESCRIPTION + " VARCHAR(255), "
            + TIME + " VARCHAR(255));";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
    Context context;

    public static mySQLHelper getmInstance(Context context) {
        if (mInstance == null)
            mInstance = new mySQLHelper(context.getApplicationContext());
        return mInstance;
    }

    public mySQLHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSION);
        this.context = context;
        Log.d("SQL", "constructor Called!");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            Log.d("SQL", "Helper onCreate called");
            db.execSQL(CREATE_TABLE);
        } catch (SQLException e) {
            Log.d("SQL", "" + e);
            Log.d("SQL", "" + e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            Log.d("SQL", "Helper onUpgrade called");
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            Log.d("SQL", "" + e);
            Log.d("SQL", "" + e);
        }

    }
}

}

Aucun commentaire:

Enregistrer un commentaire