Cordova推送通知ios

时间:2015-09-16 08:41:01

标签: cordova push-notification cordova-plugins

我正在尝试从cordova phonegap推送插件设置推送通知,该插件通过节点和命令行界面进行处理。然而,conosole说 “didRecieveNotification”和“Notification Received”但没有通过电话发出。

我的apn代码如下:

var apn = require('apn');

var options = { };

var token = 'Phone ID';

var apnConnection = new apn.Connection(options);

var myDevice = new apn.Device(token);

var message = new apn.Notification();

message.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
message.badge = 3;
message.sound = "ping.aiff";
message.alert = "\uD83D\uDCE7 \u2709 You have a new message";
message.payload = {'messageFrom': 'Caroline'};`

apnConnection.pushNotification(message, myDevice);

我的index.js尝试调用通知:

  receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);

    var pushNotification = window.plugins.pushNotification;
        if (device.platform == 'android' || device.platform == 'Android') {
            pushNotification.register(successHandler, errorHandler,{"senderID":"ID","ecb":"onNotificationGCM"});
         }
         else {
            pushNotification.register(this.tokenHandler,this.errorHandler,   {"badge":"true","sound":"true","alert":"true","ecb":"app.onNotificationAPN"});
        }
},
successHandler: function(result) {
    alert('Callback Success! Result = '+result)
},
errorHandler:function(error) {
    alert(error);
},
successHandler: function(result) {
    alert('Callback Success! Result = '+result)
},
errorHandler:function(error) {
    alert(error);
},
 onNotificationAPN: function(event) {
    var pushNotification = window.plugins.pushNotification;
    console.log("Received a notification! " + event.alert);
    console.log("event sound " + event.sound);
    console.log("event badge " + event.badge);
    console.log("event " + event);
    if (event.alert) {
        navigator.notification.alert(event.alert);
    }
    if (event.badge) {
        console.log("Set badge on  " + pushNotification);
        pushNotification.setApplicationIconBadgeNumber(this.successHandler, event.badge);
    }
    if (event.sound) {
        var snd = new Media(event.sound);
        snd.play();
    }
},

onNotificationGCM: function(e) {
        switch( e.event )
        {
            case 'registered':
                if ( e.regid.length > 0 )
                {
                    console.log("Regid " + e.regid);
                    alert('registration id = '+e.regid);
                }
            break;

            case 'message':
              // this is the actual push notification. its format depends on the data model from the push server
              alert('message = '+e.message+' msgcnt = '+e.msgcnt);
            break;

            case 'error':
              alert('GCM error = '+e.msg);
            break;

            default:
              alert('An unknown GCM event has occurred');
              break;
        }
    }

2 个答案:

答案 0 :(得分:0)

仅当应用程序位于后台时才会显示通知。尝试使用仪表板中的设备

答案 1 :(得分:0)

尝试将所有文​​件从platforms / ios / platform_www /复制到platforms / ios / www并构建应用程序。我有同样的问题,它对我有用。