javascript - pushNotification.onDeviceReady 未定义?

标签 javascript ios cordova push-notification

我正在尝试使用 Cordova 2.1.0 在我的 iOS 应用程序上运行推送通知。

我关注了this guide严格来说,但我收到以下错误:

TypeError: 'undefined' is not a function (evaluating 'pushNotification.onDeviceReady()')

我不得不稍微修改脚本以在 this guide 之后为 2.0 版修复它,这似乎有效。

我这样调用 initPushwoosh() onDeviceReady:

window.addEventListener('load', function () {
    document.addEventListener('deviceready', function () {
        initPushwoosh();
    }, false);
}, false);

PushNotification.js 文件如下:

(function(cordova) {

    function PushNotification() {}

    // Call this to register for push notifications and retreive a deviceToken
    PushNotification.prototype.registerDevice = function(config, success, fail) {
        cordova.exec(success, fail, "PushNotification", "registerDevice", config ? [config] : []);
    };

    // Call this to set tags for the device
    PushNotification.prototype.setTags = function(config, success, fail) {
        cordova.exec(success, fail, "PushNotification", "setTags", config ? [config] : []);
    };

    //Android Only----
    PushNotification.prototype.unregisterDevice = function(success, fail) {
        cordova.exec(success, fail, "PushNotification", "unregisterDevice", []);
    };

    PushNotification.prototype.startGeoPushes = function(success, fail) {
        cordova.exec(success, fail, "PushNotification", "startGeoPushes", []);
    };

    PushNotification.prototype.stopGeoPushes = function(success, fail) {
        cordova.exec(success, fail, "PushNotification", "stopGeoPushes", []);
    };

    //Android End----

    //iOS only----
    // Call this to send geo location for the device
    PushNotification.prototype.sendLocation = function(config, success, fail) {
        cordova.exec(success, fail, "PushNotification", "sendLocation", config ? [config] : []);
    };

    PushNotification.prototype.onDeviceReady = function() {
        cordova.exec(null, null, "PushNotification", "onDeviceReady", []);
    };

    // Call this to get a detailed status of remoteNotifications
    PushNotification.prototype.getRemoteNotificationStatus = function(callback) {
        cordova.exec(callback, callback, "PushNotification", "getRemoteNotificationStatus", []);
    };

    // Call this to set the application icon badge
    PushNotification.prototype.setApplicationIconBadgeNumber = function(badge, callback) {
        cordova.exec(callback, callback, "PushNotification", "setApplicationIconBadgeNumber", [{badge: badge}]);
    };

    // Call this to clear all notifications from the notification center
    PushNotification.prototype.cancelAllLocalNotifications = function(callback) {
        cordova.exec(callback, callback, "PushNotification", "cancelAllLocalNotifications", []);
    };
    //iOS End----

    // Event spawned when a notification is received while the application is active
    PushNotification.prototype.notificationCallback = function(notification) {
        var ev = document.createEvent('HTMLEvents');
        ev.notification = notification;
        ev.initEvent('push-notification', true, true, arguments);
        document.dispatchEvent(ev);
    };

    /* cordova.addConstructor(function() {
        if(!window.plugins) window.plugins = {};
        window.plugins.pushNotification = new PushNotification();
    }); */

    window.pushNotification = new PushNotification();

})(window.cordova || window.Cordova || window.PhoneGap);

function initPushwoosh()
{

    var pushNotification = window.pushNotification;
    pushNotification.onDeviceReady();

    pushNotification.registerDevice({alert:true, badge:true, sound:true, pw_appid:"17009-69D5F", appname:"Snow Day"},
                                    function(status) {
                                        var deviceToken = status['deviceToken'];
                                        console.warn('registerDevice: ' + deviceToken);
                                    },
                                    function(status) {
                                        console.warn('failed to register : ' + JSON.stringify(status));
                                        navigator.notification.alert(JSON.stringify(['failed to register ', status]));
                                    });

    pushNotification.setApplicationIconBadgeNumber(0);

    document.addEventListener('push-notification', function(event) {
                                    var notification = event.notification;
                                    navigator.notification.alert(notification.aps.alert);
                                    pushNotification.setApplicationIconBadgeNumber(0);
                              });
}

最佳答案

这很奇怪,因为来自 github 的 Pushwoosh 示例几乎是开箱即用的。

确保在 cordova 脚本之后包含 PushNotification 脚本:

    <script type="text/javascript" src="cordova-2.1.0.js"></script>
    <script type="text/javascript" src="PushNotification.js"></script>

关于javascript - pushNotification.onDeviceReady 未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700369/

相关文章:

javascript - 在 JavaScript 中获取 POST 事件而不是 Ajaxa (symfony4.1)

ios - 可以动态构建布局吗?

android - Cordova Android - 无法打开相机 - READ_EXTERNAL_STORAGE

javascript - 在 Cordova 中添加到 URL 的感叹号

Javascript Eval 是邪恶的,但仅在某些情况下?

javascript - 编写 JavaScript 库 : how to authorize methods?

javascript - 如何摆脱此 react 的 "unique key"错误

iOS - 如何设置约束,使标签的底部等于 ImageView ,但高度可以变化

ios - 从字符串转换为具有当前时区的日期

cordova - 在phonegap应用程序中自动获取OTP