c# - Pushsharp 支持 Apple Push Notification Authentication Key

标签 c# apple-push-notifications pushsharp

是否pushsharp支持新的 Apple approach使用 Apple Push Notification Authentication Key(永不过期)而不是使用证书发送 APN?有什么方法可以将它与 pushsharp 一起使用吗?如果没有,是否有任何其他 C# 库支持它?

最佳答案

你在这里:

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方法。

关于c# - Pushsharp 支持 Apple Push Notification Authentication Key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42332611/

相关文章:

c# - 插件中的 Windows 窗体

ios - 使用企业分发在 iOS 8 和 iOS 9 上推送通知

multithreading - 自托管 pushsharp - Events_OnNotificationSendFailure 失败 : There were not enough free threads in the ThreadPool to complete the operation

c# - 在 C# 中实现管道 - 使用哪些(流?)类?

c# - 跨程序集使用应用程序设置

c# - 使用依赖于另一个属性的属性初始化对象

iphone - 多个服务器使用相同的 SSL 证书连接到 APNS

ios - 确定应用程序是否正在与 APNS 沙箱或生产环境通信

ios - 无法通过临时分发的 pushsharp 发送推送通知

Azure 日志显示 : "The supplied notification payload is invalid" for official Xamarin. Android 示例