簡體   English   中英

Ionic Cordova:Push Notification插件onMessage接收消息。

[英]Ionic Cordova : Push Notification plugin onMessage receive message.

我正在使用離子科爾多瓦開發聊天應用程序。 用戶收到新消息時,我正在使用PushNotification。 問題是當用戶收到新消息時。 我的應用程序必須在服務后台更新聊天列表。 但是,離子科爾多瓦不能在后台使用。 如何在后台更新聊天列表? 我有一個想法,一旦用戶onMessage()在PushNotification插件中收到通知,便創建一個自定義插件,它將調用另一個自定義插件。 在自定義插件中,我使用urlconnection調用php服務器以從服務器獲取最新信息。 接下來,自定義插件將更新手機中的sqlite信息。 這樣做是一種好習慣嗎?

我建議您修改推送通知插件代碼。 每當您收到通知時,在GCMIntentService.java中都會檢查應用程序是處於前台還是后台。 如果應用不在前台,則使用以下語法將通知有效負載數據保存在SharedPreferences中

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("pushdata", data);

      // Commit the edits!
      editor.commit();

每當用戶打開應用程序時,請使用以下插件檢查共享的首選項並獲取存儲的數據。

cordova plugin add cordova-plugin-shared-preferences --save

獲取共享首選項的示例代碼

document.addEventListener('deviceready', () => {
  const prefs = window.plugins.SharedPreferences
  prefs.getSharedPreferences('shared_preferences', 'MODE_PRIVATE', () => {
    prefs.putString('pref_key', 'some text')

    prefs.getString('pref_key', (value) => {
      alert(value)
    }, (error) => {
      // handle error
    })
  }, (error) => {
    // handle error
  })
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM