我将FCM通知消息发送到应用程序并将消息正文和标题存储到SQLite数据库中,当我在设备连接到计算机时进行测试但是我将设备与系统分离并进行测试时,它工作正常,然后我只收到了通知消息,但没有保存到SQLite中。我无法解决确切的问题,因为当我调试代码时,它工作正常并将数据正确保存到SQLite中并向我显示适当的结果。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.d("LOGCAT", "Table created successfully");
}
// this method is used for insert the MESSAGE into the SQLite
database......
public void addInformation(String msg_title, String msg_body, SQLiteDatabase db){
db = this.getWritableDatabase();
try{
ContentValues contentValues = new ContentValues();
//contentValues.put(MESSAGE_ID,msg_ID);
contentValues.put(MESSAGE_TITLE,msg_title);
contentValues.put(MESSAGE_BODY,msg_body);
db.insert(TABLE_NAME, null, contentValues);
Log.d("SANTRIKA", "One row is inserted...");
//To check row inserted correctly or not in database.....
Long rowInserted = db.insert(TABLE_NAME,null,contentValues);
if(rowInserted != -1){
Toast.makeText(context,"One row is `inserted...",Toast.LENGTH_SHORT).show();`
}
else {
Toast.makeText(context,"Something wrong...",Toast.LENGTH_SHORT).show();
}
db.close();
}catch (Exception e){
e.printStackTrace();
}
}
// this method is used for getting all the MESSAGE from the SQLite `database into listview layout......`
public Cursor getInformation(SQLiteDatabase db){
Cursor cursor;
db = this.getReadableDatabase();
String[] projections = {MESSAGE_ID,MESSAGE_TITLE,MESSAGE_BODY};
cursor = db.query(TABLE_NAME,projections,null,null,null,null,null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// DBHelper类.........
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
SQLiteDatabase db;
DatabaseHandler databaseHandler;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data on log... it is optional....
Log.d(TAG,"From ="+remoteMessage.getFrom());
String from = remoteMessage.getFrom();
Log.d(TAG, "Notification Message Body = " + remoteMessage.getNotification().getBody());
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
//store message data into the SQLite database.....
insertMessage(title, message);
//Calling method to get notification...
sendNotification(remoteMessage.getNotification().getBody());
}
//This method is only generating push notification...
private void sendNotification(String messageBody){
String _messageBody = messageBody;
Intent intent = new Intent(this,Message_Inbox_Activity.class);
//intent.putExtra("messageBody", _messageBody);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defalutSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("SANTRIKA Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defalutSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
public void insertMessage(String title, String body){
String t= title;
String b = body;
Log.d("title", t);
Log.d("body",b);
String temp = "rajesh";
databaseHandler = new DatabaseHandler(this);
databaseHandler.addInformation(title, body, db);
}
答案 0 :(得分:0)
我在后台模式下的调试时间应用程序,所以onMessageReceived类将命中,但当app为forground(应用程序关闭)时它不会命中onMessageReceived因此你不会将数据保存到plz使用firebase的click事件首先redrect到某些然后活动将这样的数据粘贴并保存。 看我的代码。
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
if (key.equals("key")){
mid = getIntent().getExtras().getString(key);
}
if (key.equals("key")){
schoolid = getIntent().getExtras().getString(key);
}
if (key.equals("key")){
message = getIntent().getExtras().getString(key);
}
}
if (key.equals("key")){
schoolid = getIntent().getExtras().getString(key);
}
}