2016-09-26 49 views
2

我嘗試使用Firebase通知。我能夠通過this文檔正常工作。該消息已收到,我可以從MyFirebaseMessagingService服務級別向通知欄發送通知。即使應用程序處於後臺或關閉狀態,也會發生此情況。當應用程序處於後臺或關閉狀態時,無法將記錄插入SQLite數據庫中

我需要的是收集通知中發送的數據並將其插入到SQLite數據庫中。我寫的代碼工作正常,如果應用程序在前臺,但它不工作,如果它是關閉或在後臺。這是我爲插入而寫的。

DbHelper dbh=new DbHelper(this,"sample.sqlite",null,1); 
SQLiteDatabase sdb=dbh.getWritableDatabase(); 
ContentValues cv=new ContentValues(); 
cv.put("id","1"); 
cv.put("name","testname"); 
sdb.insert("test",null,cv); 
sdb.close(); 
dbh.close(); 

感謝任何幫助。提前致謝。

<service android:name=".MyFirebaseMessagingService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
    </intent-filter> 
</service> 

<service android:name=".MyFirebaseInstanceIDService"> 
    <intent-filter> 
     <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
    </intent-filter> 
</service> 

public class MyFirebaseMessagingService extends FirebaseMessagingService 
{ 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     //Displaying data in log 
     //It is optional 
     Log.i("Tag","inside message"); 
     Log.i(StaticInfo.INFO, "From: " + remoteMessage.getFrom()); 
     Log.i(StaticInfo.INFO, "Notification Message Title : " + remoteMessage.getNotification().getTitle()); 
     Log.i(StaticInfo.INFO, "Notification Message Body : " + remoteMessage.getNotification().getBody()); 

     insertPromotion(); 
     sendNotification(remoteMessage.getNotification().getBody()); 
    } 

    private void sendNotification(String messageBody) 
    { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Firebase Push Notification") 
       .setContentText(messageBody) 
       .setAutoCancel(true) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 

    private void insertPromotion() 
    { 
     DbHelper dbh = new DbHelper(this, "sample.sqlite", null, 1); 
     SQLiteDatabase sdb = dbh.getWritableDatabase(); 
     ContentValues cv=new ContentValues(); 
     cv.put("id","1"); 
     cv.put("name","testname"); 
     sdb.insert("test", null, cv); 
     sdb.close(); 
     dbh.close(); 

     Log.i("Tag","db closed"); 
    } 

} 
+0

如果當應用程序是前臺工作的,您的插入代碼完美。如果應用程序在後臺,請檢查它是否正在執行。當應用程序處於後臺時,可能會檢查您的服務正在運行。 –

+0

@SamuelRobert我檢查了它,它從後臺開始工作。當應用程序轉到後臺時,LogCat也會斷開連接。但是我確定服務在任何情況下仍然可以正常顯示。 – Deepak

+0

發佈服務的清單聲明 –

回答

4

只有當應用程序處於前臺時,通知纔會發送到您的應用程序的onMessageReceived。當您的應用程序停止運行或未運行時,系統將處理通知並將其顯示在系統托盤中。

Firebase documentation解釋它爲:

Notification消息 - FCM自動顯示給最終用戶代表客戶端應用程序的設備的消息。通知消息具有一組預定義的用戶可見密鑰。

數據消息 - 客戶端應用程序負責處理數據消息。數據消息只有自定義的鍵值對。

由於您希望始終調用您的代碼,因此您需要發送數據消息。您無法從Firebase控制檯發送數據消息。但是,如果您已經從應用程序服務器發送消息,則發送數據消息和通知消息的過程與此相同。唯一的區別是在JSON結構中,數據消息沒有notification對象。從documentation on data messages

{ 
    "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", 
    "data" : { 
    "Nick" : "Mario", 
    "body" : "great match!", 
    "Room" : "PortugalVSDenmark" 
    }, 
} 
+0

感謝您的提示,我能夠解決您的建議 – Deepak

+0

你保存了我的一天。我在我的json中都有:「通知」和「數據」對象。刪除了我的「通知」對象,它工作。 – Muno

0

爲了節省onMessgeReceived()接收到的SQLite數據庫數據,即使您的應用程序在後臺(沒有活動運行),您可以執行以下操作:

1)創建一個類延伸IntentService,例如:

public class SQLService extends IntentService { 
    private final static String MESSAGE_ID = "message_id"; 

    private MySQLiteDbAdapter mySQLiteAdapter; 

    public SQLService() { 
     super("test-service"); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     // initialize SQLite adapter here using getApplicationContext() 
     this.mySQLiteAdapter = new MySQLiteDbAdapter(getApplicationContext()); 

    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     // fetch data to save from intent 
     Message message = new Message(); 
     Message.setMessage_id(intent.getStringExtra(MESSAGE_ID)); 
     ... 
     // save 
     this.mySQLiteAdapter.add(message); 

    } 
} 

2)從onMessageReceived(啓動服務類)方法或您的火力地堡服務的延伸部內的方法,例如:

@Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 

     if (remoteMessage.getData() != null) { 

      Intent intent = new Intent(this, SQLService.class); 
      // add data to intent 
      intent.putExtra(MESSAGE_ID, remoteMessage.getData().get(MESSAGE_ID)); 
      ... 
      // start the service 
      startService(intent); 

     } 
    } 

3)在AndroidManifest.xml中註冊服務:

<application 
    ... 
     <service 
      android:name=".SQLService" 
      android:exported="false"/> 
    ... 

對於更深入的說明,請參見https://guides.codepath.com/android/Starting-Background-Services

相關問題