我必须在我的android应用中保存通知以备将来使用。我在Google上观看了youtube视频,但没有找到解决我的问题的方法。因此,我要求提供一些解决此问题的提示!!
答案 0 :(得分:0)
您可以使用SQLite将通知存储在本地数据库中。
基本上您可以做的是:
创建一个助手以在SQLite数据库中创建一个表:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
//Database Name
private static final String DATABASE = "myDatabaseName";
//Database Version
public static int VERSION = 1;
//Database tables
private static final String TABLE_NOTIFICATIONS = "notifications";
//Notification fields
private static final String KEY_ID = "id";
//Notifications Table
private static final String KEY_DESCRIPTION = "description";
public DatabaseHelper(Context context){
super(context, DATABASE, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_NOTIFICATIONS_TABLE = "CREATE TABLE " + TABLE_NOTIFICATIONS + "(" +
KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_DESCRIPTION + " VARCHAR(255) "
+");";
db.execSQL(CREATE_NOTIFICATIONS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICATIONS);
onCreate(db);
}
}
为其创建bean:
public class NotificationBean {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
我还建议创建一个DAO以完成整个过程:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.yourpackage.beans.NotificationBean;
import com.yourpackage.helpers.DatabaseHelper;
import java.util.ArrayList;
import java.util.List;
public class NotificationDao {
private DatabaseHelper helper;
private SQLiteDatabase database;
private ContentValues contentValues;
public NotificationDao(Context context){
helper = new DatabaseHelper(context);
}
private SQLiteDatabase getDatabase(){
if(database == null){
database = helper.getWritableDatabase();
}
return database;
}
public void close(){
helper.close();
}
public void addNotification(NotificationBean notificationBean){
contentValues = new ContentValues();
contentValues.put("description", notificationBean.getDescription());
getDatabase().insert("notifications", null, contentValues);
}
public List<NotificationBean> getNotifications(){
List<NotificationBean> notificationBeanList = new ArrayList<>();
final String SELECT_QUERY = "SELECT description FROM notifications";
Cursor cursor = getDatabase().rawQuery(SELECT_QUERY, null);
if(cursor.moveToFirst()){
do{
NotificationBean notificationBean = new NotificationBean();
notificationBean.setDescription(cursor.getString(0));
notificationBeanList.add(notificationBean);
}while (cursor.moveToNext());
}
return notificationBeanList;
}
}
创建后,只需在通知服务中调用它即可
public class NotificationsListenerService extends FirebaseMessagingService {
private NotificationDao notificationDao;
private NotificationBean notificationBean;
@Override
public void onMessageReceived(RemoteMessage message) {
//Saving notification on database
notificationDao = new NotificationDao(NotificationsListenerService.this);
notificationBean = new NotificationBean();
notificationBean.setDescription(message.getNotification().getBody());
notificationDao.addNotification(notificationBean);
}
}
请记住,您可以为表添加任何想要的字段,这只是我在某些项目中使用的。但是,您也可以添加created_at字段,以跟踪添加通知的日期。
就是这样,如果您还有其他疑问,请告诉我