Я отправляю уведомление на устройства, использующие firebase, но я не могу сохранить детали этого уведомления в sqlite. Я использую вид карты, чтобы показать уведомление в списке. У меня есть аналогичный код, который хранит закладки в той же базе данных и работает нормально.
Создание базы данных DBHelper. java
package ak.wp.meto.data.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
private static DbHelper dbHelper = null;
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "web_aoo.db";
public static DbHelper getInstance(Context context) {
if (dbHelper == null) {
dbHelper = new DbHelper(context);
}
return dbHelper;
}
private DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(DbConstants.SQL_CREATE_NOTIFICATION_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DbConstants.SQL_DELETE_NOTIFICATION_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
Для управления уведомлением чтения / непрочитанных & для записи уведомления в базу данных NotificationDbController. java
package ak.wp.meto.data.sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ak.wp.meto.models.NotificationModel;
import java.util.ArrayList;
public class NotificationDbController {
private SQLiteDatabase db;
private static final String READ = "read", UNREAD = "unread";
public NotificationDbController(Context context) {
db = DbHelper.getInstance(context).getWritableDatabase();
}
public int insertData(String title, String message, String contentUrl) {
ContentValues values = new ContentValues();
values.put(DbConstants.COLUMN_NOT_TITLE, title);
values.put(DbConstants.COLUMN_NOT_MESSAGE, message);
values.put(DbConstants.COLUMN_NOT_READ_STATUS, UNREAD);
values.put(DbConstants.COLUMN_NOT_CONTENT_URL, contentUrl);
// Insert the new row, returning the primary key value of the new row
return (int) db.insert(
DbConstants.NOTIFICATION_TABLE_NAME,
DbConstants.COLUMN_NAME_NULLABLE,
values);
}
public ArrayList<NotificationModel> getAllData() {
String[] projection = {
DbConstants._ID,
DbConstants.COLUMN_NOT_TITLE,
DbConstants.COLUMN_NOT_MESSAGE,
DbConstants.COLUMN_NOT_READ_STATUS,
DbConstants.COLUMN_NOT_CONTENT_URL
};
// How you want the results sorted in the resulting Cursor
String sortOrder = DbConstants._ID + " DESC";
Cursor c = db.query(
DbConstants.NOTIFICATION_TABLE_NAME, // The table name to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
return fetchData(c);
}
public ArrayList<NotificationModel> getUnreadData() {
String[] projection = {
DbConstants._ID,
DbConstants.COLUMN_NOT_TITLE,
DbConstants.COLUMN_NOT_MESSAGE,
DbConstants.COLUMN_NOT_READ_STATUS,
DbConstants.COLUMN_NOT_CONTENT_URL
};
// How you want the results sorted in the resulting Cursor
String sortOrder = DbConstants._ID + " DESC";
String selection = DbConstants.COLUMN_NOT_READ_STATUS + "=?";
String[] selectionArgs = {UNREAD};
Cursor c = db.query(
DbConstants.NOTIFICATION_TABLE_NAME, // The table name to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
return fetchData(c);
}
private ArrayList<NotificationModel> fetchData(Cursor c) {
ArrayList<NotificationModel> ntyDataArray = new ArrayList<>();
if (c != null) {
if (c.moveToFirst()) {
do {
// get the data into array,or class variable
int itemId = c.getInt(c.getColumnIndexOrThrow(DbConstants._ID));
String title = c.getString(c.getColumnIndexOrThrow(DbConstants.COLUMN_NOT_TITLE));
String message = c.getString(c.getColumnIndexOrThrow(DbConstants.COLUMN_NOT_MESSAGE));
String status = c.getString(c.getColumnIndexOrThrow(DbConstants.COLUMN_NOT_READ_STATUS));
String contentUrl = c.getString(c.getColumnIndexOrThrow(DbConstants.COLUMN_NOT_CONTENT_URL));
boolean isUnread = !status.equals(READ);
// wrap up data list and return
ntyDataArray.add(new NotificationModel(itemId, title, message, isUnread, contentUrl));
} while (c.moveToNext());
}
c.close();
}
return ntyDataArray;
}
public void updateStatus(int itemId, boolean read) {
String readStatus = UNREAD;
if (read) {
readStatus = READ;
}
// New value for one column
ContentValues values = new ContentValues();
values.put(DbConstants.COLUMN_NOT_READ_STATUS, readStatus);
// Which row to update, based on the ID
String selection = DbConstants._ID + "=?";
String[] selectionArgs = {String.valueOf(itemId)};
db.update(DbConstants.NOTIFICATION_TABLE_NAME, values, selection, selectionArgs);
}
}
Для извлечения уведомлений в просмотре карты и отображения их в списке. NotificationDetailsActivity. java
package ak.wp.meto.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import ak.wp.meto.R;
import ak.wp.meto.data.constant.AppConstant;
import ak.wp.meto.utility.ActivityUtils;
public class NotificationDetailsActivity extends BaseActivity {
private Context mContext;
private Activity mActivity;
private TextView titleView, messageView;
private Button linkButton;
private String title, message, postId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity = NotificationDetailsActivity.this;
mContext = mActivity.getApplicationContext();
initView();
initVeritable();
initFunctionality();
initListeners();
}
private void initVeritable() {
Bundle extras = getIntent().getExtras();
title = extras.getString(AppConstant.BUNDLE_KEY_TITLE);
message = extras.getString(AppConstant.BUNDLE_KEY_MESSAGE);
postId = extras.getString(AppConstant.BUNDLE_KEY_POST_ID);
}
private void initView() {
setContentView(R.layout.activity_notification_details);
titleView = (TextView) findViewById(R.id.title);
messageView = (TextView) findViewById(R.id.message);
linkButton = (Button) findViewById(R.id.linkButton);
initToolbar();
setToolbarTitle(getString(R.string.notifications));
enableBackButton();
}
private void initFunctionality() {
titleView.setText(title);
messageView.setText(message);
if (postId != null && !postId.isEmpty()) {
linkButton.setEnabled(true);
} else {
linkButton.setEnabled(false);
}
}
private void initListeners() {
linkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = -1;
try {
id = Integer.parseInt(postId);
} catch (Exception e) {
}
ActivityUtils.getInstance().invokePostDetails(mActivity, PostDetailsActivity.class, id, false);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onBackPressed() {
finish();
}
}