我在向ios设备发送推送通知时收到errorNum:8

前端之家收集整理的这篇文章主要介绍了我在向ios设备发送推送通知时收到errorNum:8前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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);
            });
        }
    }
}

Here is my code. In console I’m getting all the stuff and device token is also fine. Android mobiles are getting notifications. But notifications are not sending to ios devices. I’m getting this error in console : Error is:…… 8.
One more thing is,for the same device I’m able to send the notification for other functionality with other code.
Really I’m pulling my hair out for this issue. And can’t understand what’s wrong with my code. Anyone please give solution for this.

解决方法

您使用的是旧版本.去年3月,苹果公司在推送api中改变了一些东西.
此外,你忘了设置你的主题,这是apn推送通知的mandetory

如果(devicetype ==“ios”)阻止,请尝试这样的事情

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,'id': id};

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

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

    //,"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

猜你在找的iOS相关文章