Pushsharp支持Apple推送通知身份验证密钥

时间:2017-02-19 20:38:28

标签: c# apple-push-notifications pushsharp

pushsharp是否支持使用 Apple推送通知身份验证密钥(永不过期)而不是使用证书来发送APN的新Apple approach?有没有办法与pushsharp一起使用它?如果没有,是否还有其他C#库来支持它?

1 个答案:

答案 0 :(得分:1)

你在这里:

private string GetToken()
{
    var algorithm = "ES256";
    var teamID = "teamID";
    var apnsKeyID = "apnsKeyID";
    var apnsAuthKeyPath = @"apnsAuthKeyPath";
    var epochNow = DateTimeOffset.Now.ToUnixTimeSeconds();

    var header = new Dictionary<string, object>()
    {
        { "alg", algorithm },
        { "kid" , apnsKeyID }
    };
    var payload = new Dictionary<string, object>()
    {
        { "iss", teamID },
        { "iat", epochNow }
    };

    var privateKey = GetPrivateKey(apnsAuthKeyPath);
    var token = Jose.JWT.Encode(payload, privateKey, algorithm: Jose.JwsAlgorithm.ES256, extraHeaders: header);
    return token;
}
private CngKey GetPrivateKey(string apnsAuthKey)
{
    using (var reader = File.OpenText(apnsAuthKey))
    {
        var ecPrivateKeyParameters = (ECPrivateKeyParameters)new PemReader(reader).ReadObject();
        var x = ecPrivateKeyParameters.Parameters.G.AffineXCoord.GetEncoded();
        var y = ecPrivateKeyParameters.Parameters.G.AffineYCoord.GetEncoded();
        var d = ecPrivateKeyParameters.D.ToByteArrayUnsigned();
        return EccKey.New(x, y, d);
    }
}

然后你可以在apns中调用send方法。