【问题标题】:Detecting push notification event in cordova app after cold start (from notification click)冷启动后检测cordova应用程序中的推送通知事件(从通知点击)
【发布时间】:2018-01-27 11:35:39
【问题描述】:

我的推送通知包含自定义数据,有助于在单击通知时将应用程序路由到正确的位置。如果应用程序打开或在后台运行,这些工作正常,但在应用程序关闭时不起作用。通知会打开应用程序,但我的代码没有检测到引用的通知事件,因此我可以路由它。 我在 AppDelegate.m 中有这个

#ifndef DISABLE_PUSH_NOTIFICATIONS

    - (void)                                 application:(UIApplication*)application
        didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
    {
        // re-post ( broadcast )
        NSString* token = [[[[deviceToken description]
            stringByReplacingOccurrencesOfString:@"<" withString:@""]
            stringByReplacingOccurrencesOfString:@">" withString:@""]
            stringByReplacingOccurrencesOfString:@" " withString:@""];

        [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:token];
    }

    - (void)                                 application:(UIApplication*)application
        didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
    {
        // re-post ( broadcast )
        [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotificationError object:error];
    }
#endif

我有这个 JS 代码可以在我的应用程序中捕获通知

document.addEventListener('push-notification', function(event) {
                             //get the notification payload

                             console.log("incoming PUSH notification!")
                             console.log(event);
                             factory.reactToIncomingPushNotification(event)


                         });

我的问题是应用冷启动后如何在应用中引用通知事件?

我有一个以 pushwoosh 作为提供者的 cordova/ionic 应用程序。我正在IOS上测试。

【问题讨论】:

    标签: ios cordova push-notification pushwoosh


    【解决方案1】:

    我遇到了类似的问题,因此我尝试尽早将事件侦听器放入 Javascript 中。然后我能够收到来自 Pushwoosh 的推送通知。这意味着 DOM 事件在您应用的 JS 中的“推送通知”侦听器设置之前触发。

    使用 setTimeout 似乎可以通过延迟 DOM 事件并让侦听器做好准备来掩盖问题。

    我建议尽早移动听者。

    document.addEventListener('push-notification', function(event) {
        ...
    })
    

    但是,这可能不是主意,因为应用程序可能尚未准备好使用推送通知中的事件。我的解决方案是缓存事件并在我的应用程序完全启动后检查它。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      我的答案是在 pushwoosh 广播事件上设置超时。在我设置监听器之前它正在发送广播。这只发生在冷启动应用程序上。现在,我检测推送通知是否将“onStart”键设置为 true。如果是这样,我在 dispatchEvent 函数上设置了 1.3 秒的超时时间。这是 PushNotification.js 文件的修改函数。

      // file: plugins/pushwoosh-cordova-plugin/www/PushNotification.js 
      PushNotification.prototype.notificationCallback = function(notification) {
          var ev = document.createEvent('HTMLEvents');
          ev.notification = notification;
          // test for the cold start
          var tout = notification.onStart ? 1300 : 10 ;
          setTimeout(function() { 
              ev.initEvent('push-notification', true, true, arguments);
              document.dispatchEvent(ev);
          }, tout);
      };
      

      【讨论】:

        【解决方案3】:

        如果应用程序被终止,并且用户想要自定义数据点击通知,那么 AppDelegate 的 didFinishLaunchingWithOptions 是您的方法,因为它包含名为 UIApplicationLaunchOptionsRemoteNotificationKey 的键中的数据

        【讨论】:

        • 谢谢。我看到了该功能(并将其发布在我的问题中)。但是,我相信这个功能(打开时将通知数据发送到应用程序的能力)已融入 pushwoosh 插件代码中。我正在尝试通过 pushwoosh delegate.m 文件跟踪通知,但我迷路了。无法弄清楚为什么 JS 监听器没有被触发。
        猜你喜欢
        • 2014-12-16
        • 2015-11-17
        • 1970-01-01
        • 2013-04-29
        • 2016-10-30
        • 2021-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多