9

Does pushsharp supports a new Apple approach for sending APN using Apple Push Notification Authentication Key (which never expires) instead of using Certificates? Is any way to use it with pushsharp? If not, is there any other C# library to support it?

Mike Keskinov
  • 10,359
  • 5
  • 53
  • 73
  • Hi, may I know if you have found the answer to this? Thanks. – TPG Mar 14 '17 at 01:55
  • 2
    No. I think nothing is supported so far. I just keep using the old code and certificates for now. Hope they will add this capability in the future – Mike Keskinov Mar 14 '17 at 22:22

1 Answers1

1

here you are:

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);
    }
}

then you can simply call send method in apns.

Mohammad
  • 2,482
  • 5
  • 24
  • 48