Cordova 推送通知 ios

Posted

技术标签:

【中文标题】Cordova 推送通知 ios【英文标题】:Cordova Push Notification ios 【发布时间】:2015-09-16 08:41:01 【问题描述】:

我正在尝试从 cordova phonegap 推送插件设置推送通知,该插件通过节点和命令行界面进行处理。然而控制台说 “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;
        
    

【问题讨论】:

【参考方案1】:

仅当应用程序在后台时才会显示通知。尝试使用仪表板中的设备进行操作

【讨论】:

【参考方案2】:

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

【讨论】:

以上是关于Cordova 推送通知 ios的主要内容,如果未能解决你的问题,请参考以下文章

如何将推送通知添加到 iOS 应用程序(cordova-plugin-firebasex)?

在 ionic cordova 和 firebase 中为 iOS 应用程序设置推送通知

需要在 Cordova 应用程序中使用本机 APNs/GCM 为 iOS 推送通知的指导

如何在 Cordova iOS 应用程序中实现推送通知

iOS 8 和 Cordova:应用程序在首次启动时立即请求推送通知权限

解析推送通知:iOS 设备令牌未定义