2017-03-10 6 views
0

私はFCM通知メッセージをアプリケーションに送信し、メッセージ本体とタイトルをSQLiteデータベースに保存しますが、デバイスがコンピュータに接続されているときにテストすると、デバイスからシステムを切り離してテストすると、通知メッセージだけが表示されますが、SQLiteには保存されません。私は正確な問題は何もできません。なぜなら、コードをデバッグするときに問題なく動作し、データをSQLiteに適切に保存し、適切な結果を表示するからです。FCM通知メッセージの値がモバイルの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); 
} 

// classs .........

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); 

} 
+1

コードを表示する必要があります。データを保存するためにCPUを起動する必要があるかもしれません。 –

+0

デバッガとしてのモバイル時のテストはすべてうまく動作しますが、モバイルをコンピュータから削除してモバイルに表示された通知を問題なく送信しますが、通知メッセージはsqliteに保存されません –

+0

デバッグ時にカーソルを取得しました –

答えて

0

Iバックグラウンドモードでのデバッグ時のアプリの事そうonMessageReceivedクラスがヒットしますが、アプリがforgroundされる(アプリDBHelperそれがonMessageReceivedには当たらないので、あなたはfirebaseのclickイベントを使用してデータを保存しません。まず、あるアクティビティを再修正し、このようなデータをタックして保存します。 私のコードを見てください。

  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); 
      } 


     } 
+0

答えてくれてありがとう、実際には私のアプリケーションをフォアグラウンドで(画面上で)開いたままにして、FCMコンソールから通知メッセージを送信しても通知メッセージだけを受け取ることができましたが、メッセージタイトルと本文をsqliteデータベース。 –

+0

あなたはどのようにデータがアプリケーションがデバッグモードであるときに格納されると言う –

+0

デバッグモードでデータを取得しました –

関連する問題