【问题标题】:Ionic Cordova : Push Notification plugin onMessage receive message.Ionic Cordova:Push Notification 插件 onMessage 接收消息。
【发布时间】:2017-08-05 20:21:11
【问题描述】:

我正在使用 ionic cordova 开发一个聊天应用程序。当用户收到新消息时,我正在使用 PushNotification。问题是当用户收到新消息时。我的应用程序必须在服务后台更新聊天列表。但是,ionic cordova 无法在后台进行服务。如何在后台更新聊天列表?一旦用户 onMessage() 在 PushNotification 插件中收到通知,我有一个想法创建一个自定义插件,它将调用另一个自定义插件。在自定义插件中,我使用 urlconnection 调用 php 服务器以从服务器获取最新信息。接下来,自定义插件正在更新手机中的 sqlite 信息。这样做是一种好习惯吗?

【问题讨论】:

    标签: android cordova push-notification cordova-plugins


    【解决方案1】:

    我建议您修改推送通知插件代码。每当您收到通知时,在 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
      })
    }
    

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2017-03-28
      • 2014-12-10
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多