【问题标题】:I'm getting the errorNum :8 while sending the push notifications to ios devices我在向 ios 设备发送推送通知时收到 errorNum :8
【发布时间】:2018-03-09 09:35:07
【问题描述】:

var apn = require('apn');
var gcm = require('android-gcm');


export default function notification( devicetype, devicetoken, alert, userid, action, profilepic,  image, youtubeimage, id ) {

   
    if(devicetoken != "(null)") {
        var androidApiKey = '', cert = '', key = '', passphrase = '';

        if(process.env.NODE_ENV.toLowerCase() == "production") {
          cert = '/../config/ios_support/apns-cert.pem';
          key = '/../config/ios_support/apns-key.pem';
          passphrase = '*****';
          androidApiKey = "*******";
        }
        else {
          cert = '/../config/ios_support/apns-dev-cert.pem';
          key = '/../config/ios_support/apns-dev-key.pem';
          passphrase = '*******';
          androidApiKey = "********";
        }


        if(devicetype == "ios"){
            var myDevice = new apn.Device(devicetoken);

            var note = new apn.Notification();
            note.badge = 1;
            note.sound = "notification-beep.wav";

            note.alert = alert;

            note.category = "respond"

            note.device = myDevice;

            note.payload = { 'action': action, 'userid': userid, 'profilepic': profilepic, 'id':id};

            console.log("note.payload: "+ JSON.stringify(note.payload));

            //, 'WatchKit Simulator Actions': [{"title": "Show", "identifier": "showButtonAction"}]

            var callback = function (errorNum, notification) {
                console.log('Error is:.....', errorNum);
            }


            var options = {
                gateway: 'gateway.push.apple.com',
                //'gateway.sandbox.push.apple.com',
                // this URL is different for Apple's Production Servers and changes when you go to production
                errorCallback: callback,
                cert: __dirname.split('src/')[0] + cert,
                key: __dirname.split('src/')[0] + key,
                passphrase: passphrase,
                port: ****,
                cacheLength: 100
            }

            var apnsConnection = new apn.Connection(options);
            apnsConnection.sendNotification(note);
        }
        else if(devicetype == "android"){

            var gcmObject = new gcm.AndroidGcm(androidApiKey);
            var message = new gcm.Message({
                registration_ids: [devicetoken],
                data: {
                    body: alert,
                    action: action,
                    userid: userid,
                    profilepic: profilepic,
                    id: id
                }
            });

            gcmObject.send(message, function(err, response) {
                if(err) console.error("error: "+err);
        //        else    console.log("response: "+response);
            });
        }
    }
}

这是我的代码。在控制台中,我得到了所有的东西,设备令牌也很好。 Android 手机正在收到通知。但通知不会发送到 ios 设备。我在控制台中收到此错误:错误是:...... 8。 另一件事是,对于同一设备,我可以使用其他代码发送其他功能的通知。 真的,我正在为这个问题拉头发。并且无法理解我的代码有什么问题。请任何人为此提供解决方案。

【问题讨论】:

  • 请与您的服务器联系support.apple.com/en-in/HT203609
  • 感谢@PPL 的回复。我正在使用端口:2195。
  • 我测试过:github.com/noodlewerk/NWPusher。从推送器进行测试时正在发送通知。这意味着证书很好,设备令牌也很好。但是在使用我的代码通知时没有发送。我的代码有什么问题??

标签: ios node.js npm apple-push-notifications apn


【解决方案1】:

您使用的是旧版本。我猜苹果去年 3 月在 push api 中改变了一些东西。 你也忘了设置你的topic 这是apn推送通知的强制要求

为你尝试这样的事情if (devicetype == "ios") block

if(devicetype == "ios") {
    var myDevice = new apn.Device(devicetoken);

    var note = new apn.Notification();
    note.badge = 1;
    note.sound = "notification-beep.wav";
    note.alert = alert;
    note.category = "respond"
    note.payload = {'action': action, 'userid': userid, 'profilepic': profilepic, 'id': id};

    //you missed this one i guess
    note.topic = "<your-app-bundle-id>";

    console.log("note.payload: " + JSON.stringify(note.payload));

    //, 'WatchKit Simulator Actions': [{"title": "Show", "identifier": "showButtonAction"}]

    var callback = function(errorNum, notification) {
        console.log('Error is:.....', errorNum);
    }


    var options = {
        token: {
            key: __dirname.split('src/')[0] + cert,
            keyId: __dirname.split('src/')[0] + key,
            teamId: "developer-team-id"
        },
        production: false // for development
    };

    var apnProvider = new apn.Provider(options);

    apnProvider.send(note, myDevice).then( (result) => {
        // see documentation for an explanation of result
        console.log(result);
    });

}

你可以在这里找到文档apn

【讨论】:

  • 嗨@BraveButter,问题已经解决。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多