我想要做的是當我向Android應用程序發送通知(使用Firebase)時,我將通知消息存儲在SQLite表中,以讓用戶在ListView中查看他的通知。SQLite不存儲所有數據
問題是,只有當應用程序位於前臺時,SQLite纔會存儲通知文本,否則手機會收到通知,但不會保存它。
我的數據庫中創建名爲類:DBController
public DBContoller(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, "cloudmessaging.db", factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Messages(MSG TEXT,created_at DATETIME DEFAULT CURRENT_TIMESTAMP);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Messages");
}
ContentValues cv;
//add
public void add(String MSG){
cv=new ContentValues();
cv.put("MSG",MSG);
this.getWritableDatabase().insertOrThrow("Messages", "", cv);
}
public void selectAll(List lst){
Cursor cursor= this.getReadableDatabase().rawQuery("SELECT * FROM Messages order by created_at DESC ",null);
lst.clear();
while(cursor.moveToNext()){
lst.add("Message : "+cursor.getString(0)+" - ["+cursor.getString(1)+" ]");
}
}
我如何保存通知消息:
NotificationCompat.Builder notifBuilder=new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Gi2017 :)")
.setContentText(body)
.setAutoCancel(true)
.setSound(notifsound)
.setContentIntent(pendingintent);
NotificationManager notifManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.notify(0/*notification ID*/,notifBuilder.build());
try{
DBContoller db=new DBContoller(getApplicationContext(),"",null,1);
db.add(body);
}catch(Exception e){Log.d("error DB",e.getMessage());}
我如何表現存儲的消息:
DBContoller db;
ListView lstAfficher;
db=new DBContoller(getApplicationContext(),"",null,1);
lstAfficher = (ListView) findViewById(R.id.listView);
List<String> lst = new ArrayList<String>();
db.selectAll(lst);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.listlayout,lst);
lstAfficher.setAdapter(adapter);
你是否看到任何說「錯誤數據庫」的logcat消息? – Karakuri
你可以發佈你發送的樣本有效載荷嗎? :) –
@Karakuri沒有錯誤 – CrazyDeveloper