Require.js with Phonegap and Push Notification for iOs

隐身守侯 提交于 2019-12-02 06:04:42

I had similar problems, just that my onNotificationAPN didn't get called. I used this guide as a reference (to setting up the register-call) - Push Notifications guide

Try using the guides way to add the callback function. You can also have a look at my push notification handler as a requirejs module. It works fine :) Btw, I'm using Durandal with knockout for building my app.

In my index.html I have a reference to PushNotification.js and that file is also in my project.

Index.html:

<body>
 <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
 <script type="text/javascript" src="Scripts/jquery/jquery-2.0.3.min.js"></script>
 <!-- PhoneGap plugins -->
 <script type="text/javascript" charset="utf-8" src="Scripts/phoneGap/PushNotification.js"></script>
....
<script type="text/javascript" src="Scripts/require.js"></script>
<script>
   var useragent = navigator.userAgent.toLowerCase();
 if (useragent.match(/android/) || useragent.match(/iphone/) || useragent.match(/ipad/) || useragent.match('ios')) {
document.addEventListener('deviceready', onDeviceReady, false);
    }
    else {
        onDeviceReady();
    }

    function onDeviceReady() {
        ....

        require.config({
            baseUrl: 'App',
            paths: {
                "main": "main"
            }
        });
        require(["main"]);
    };
</script>

And the push notification module:

define([
'knockout'
], function (
ko
) {
var pushNotification = window.plugins.pushNotification;

function addCallback(key, callback) {
    if (window.callbacks === undefined) {
        window.callbacks = {};
    }
    window.callbacks[key] = callback;
};

function registerDevice() {
    pushNotification.register(
    tokenHandler,
    errorHandler, {
        "badge": "true",
        "sound": "false",
        "alert": "true",
        "ecb": "callbacks.notificationHandler"
    });
};

// result contains any message sent from the plugin call
function successHandler(result) {
    alert('result = ' + result);
};

// result contains any error description text returned from the plugin call
function errorHandler(error) {
    alert('error = ' + error);
};

function tokenHandler(result) {
    // Your iOS push server needs to know the token before it can push to this device
    // here is where you might want to send it the token for later use.
    console.log('post token to rikardo', result);

    svc.post('Notification', ko.toJSON({ DeviceToken: result }));
    addCallback('notificationHandler', onNotificationAPN);
};

// iOS
function onNotificationAPN(event) {
    var model = {},
        type = event.type;

    if (event.inAppMessage)
        model = JSON.parse(event.inAppMessage);

    if (type == 'AchievementViewModel') {
        pushModalHandler.addItem(model);
        pushModalHandler.displayModals('achievement');
    }

    if (type == 'TimeQuestViewModel') {
        pushModalHandler.addItem(model);
        pushModalHandler.displayModals('timeQuest');
    }
};

return {
    registerDevice: registerDevice
};
});

I hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!