OneSignal Wordpress Notifications未在App中打开

时间:2018-02-02 05:19:42

标签: wordpress cordova onesignal

我很想找到我的Cordova Native App的解决方案。

我正在使用OneSignal在新文章发布后发送推送通知。推送通知工作正常,但是当我点击通知时,普通浏览器打开而不是应用程序。

注意:当我通过OneSignal发送测试通知并单击它时,应用程序将打开。 我的代码只是不想使用Wordpress文章。

这是我在index.js中使用的代码

document.addEventListener('deviceready', function () {
  // Enable to debug issues.
  // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});

var notificationOpenedCallback = function(jsonData) {
  var additionalData = jsonData.notification.payload.additionalData
  if (additionalData && additionalData.myKey)
    // Not familiar with Cordova, $state may or may not be available here
    $state.go('app.post', {'postId': + additionalData.myKey});
};

  window.plugins.OneSignal
    .startInit("0b8b7e69-1649-4616-XXXXXXX")
    .handleNotificationOpened(notificationOpenedCallback)
    .endInit();

  // Call syncHashedEmail anywhere in your app if you have the user's email.
  // This improves the effectiveness of OneSignal's "best-time" notification scheduling feature.
  // window.plugins.OneSignal.syncHashedEmail(userEmail);
}, false);

有人知道我的代码可能有什么问题吗?

2 个答案:

答案 0 :(得分:1)

您需要将所有代码放在插件下检查设备就绪功能,并确保将数据解析为数组对象,因为它们发送的原始内容如下所示 -

if (window.plugins && window.plugins.OneSignal) {
    .startInit(your_appid, your_google_project)
    .inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.Notification)
    .handleNotificationOpened(function(jsonData) {
        if (device.platform =="Android") {
            var addData = JSON.parse(jsonData.result.notification.payload.additionalData);
        } else {
            var addData = jsonData.result.notification.payload.additionalData;
        }
        if (addData != undefined) {
            if (addData.posts != undefined) {
                var postID = Number(addData.posts);
                $state.go('app.post', {postId: postID });
            }
        }
    })
    .endInit();
}

注意:根据该文档,他们将附加数据树更新为

result.notification.payload.additionalData

不是notification.payload.additionalData

将此功能放在wordpress主题function.php上,并确保您已禁用选项"另外向iOS&发送通知Android平台"。在WordPress插件页面上。

add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);

function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
    $postID = $post->id;
    $fields['isAndroid'] = true;
    $fields['isIos'] = true;
    $fields['isAnyWeb'] = false;
    $fields['isChrome'] = false;
    $fields['data'] = array(
       "posts" => $postID
   );
   return $fields;
}

答案 1 :(得分:0)

只要其他人可以使用它,就把它留在这里。

add_filter('onesignal_send_notification', 'onesignal_send_notication_filter_push', 10, 4);

function onesignal_send_notication_filter_push($fields, $new_status, $old_status, $post)
{
    $fields['isAndroid'] = true;
    $fields['isIos'] = true;
    $fields['isAnyWeb'] = false;
    $fields['isChrome'] = false;
    // overwrite http url with app_url so push notifications opens app instead of phone browser. 
    $fields['url'] = $fields['app_url'];

    return $fields;
}