0

我想要做的是當我向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); 
+0

你是否看到任何說「錯誤數據庫」的logcat消息? – Karakuri

+0

你可以發佈你發送的樣本有效載荷嗎? :) –

+0

@Karakuri沒有錯誤 – CrazyDeveloper

回答

0

你最有可能發送通知消息。

通知郵件,當你想FCM處理顯示代表您的客戶端應用程序的通知:由於documentation for Firebase Cloud Messaging說,通知消息由系統,當你的應用程序被轉到後臺處理。當您想要處理客戶端應用程序中的消息時使用數據消息。

如果你總是希望你的應用程序代碼得到消息,你應該發送一個data message

+0

嘿感謝您的支持,嘗試使用數據,通知即使應用程序死亡或後臺成功到達,但數據(方法onMessageReceived())只有當應用程序在屏幕上時才存儲。 – CrazyDeveloper

0
You can create table like this. 


final String NOTIFICATION_SAVE = "CREATE TABLE " + NOTIFICATION_GROUPING + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE," + FEED_ID + " TEXT," + MESSAGE + " TEXT," + ACTION + " TEXT)"; 
     db.execSQL(NOTIFICATION_SAVE); 


    And store data into created table like this. 

SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 
     cv.put(FEED_ID , "102"); 
     cv.put(MESSAGE, "notification"); 
     db.insert(NOTIFICATION_GROUPING, null, cv); 
     db.close();