【问题标题】:Why is my notification event not firing in Cordova?为什么我的通知事件没有在 Cordova 中触发?
【发布时间】:2020-02-09 23:28:06
【问题描述】:

我正在从 Amazon SNS 发布通知并在我的设备上正确接收它。

这是我要发送的示例消息:

{
  "APNS_SANDBOX": "{\"aps\":{\"alert\":{\"title\":\"A short string describing the purpose of the notification\",\"body\":\"The text of the alert message\",\"launch-image\":\"The filename of an image file in the app bundle, with or without the filename extension. The image is used as the launch image when users tap the action button or move the action slider\"},\"badge\":5,\"content-available\":\"1\",\"category\":\"identifier\",\"thread-id\":\"id\",\"sound\":\"default\"},\"notId\":1,\"custom_key1\":\"value1\",\"custom_key2\":\"value2\"}"
}

但是,当我在手机上打开通知时,我期待 notification 事件触发,我的目标是在打开通知时向用户发送一个 url,例如:window.location='data.some_link_to_notification'; 我把所有这些都去掉了,只是添加了一个警报,看看它是否被调用,但似乎没有。这是我的index.js 文件。

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    onDeviceReady: function() {
      StatusBar.backgroundColorByHexString('#FFFFFF');

      var push = PushNotification.init({
        android: {},
        browser: {},
        ios: {
          alert: "true",
          badge: "true",
          sound: "true"
        },
        windows: {}
      });

      push.on('notification', function (data) {
        alert("notification event");
      });

      push.on('error', function (err) {
        console.log(err)
        alert('Event=error, message=' + err.message)
      });

      push.on('registration', function (data) {
        console.dir(data)
        console.log('registrationId:' + data.registrationId)

       window.localStorage.setItem("regId", data.registrationId);
      });

     cordova.InAppBrowser.open('app_url', '_self');
    }
};

app.initialize();

【问题讨论】:

  • 原来调用cordova.InAppBrowser.open('app_url', '_self')会中断链接到我的cordova.js文件,所以现在所有的监听器都被中断了。使用 InAppBrowser 不是常用方法吗?

标签: ios cordova push-notification apple-push-notifications amazon-sns


【解决方案1】:

正如您在评论中所说,您的问题是使用cordova.InAppBrowser.open('app_url', '_self'); 加载远程网址

当你导航到不同的页面时(不管是远程页面还是本地页面),所有的 javascript 代码都会丢失,所以当通知到达时会触发通知事件,但是什么都没有听。

由于它是一个远程页面,你还有另一个问题,cordova.js 或任何插件 javascript 都不在线,如果你想让 Cordova 插件从远程 url 工作,你必须上传 iOS cordova.js, plugins 文件夹和更多文件(如果您在platforms/ios/ 上打开Xcode 项目,您会看到一个黄色的“Stagin”文件夹,里面有一个蓝色的“www”文件夹,您应该将所有代码上传到您的服务器。 请记住,代码仅适用于 iOS,因此如果您想对 Android 执行相同操作,则应在服务器中为每个代码使用不同的 url。并从远程版本中删除cordova.InAppBrowser.open('app_url', '_self');,否则会导致无限循环。

另外请记住,Apple 不喜欢加载外部网站的应用,如果他们注意到,他们可能会拒绝您的应用。

【讨论】:

  • 感谢您的信息,您已经确认了过去 2 天阅读的很多内容!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-18
  • 2012-01-11
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
相关资源
最近更新 更多