net-core-push-notifications icon indicating copy to clipboard operation
net-core-push-notifications copied to clipboard

Exception when sending notifications to iOS devices

Open arnel-sanchez opened this issue 2 years ago • 1 comments

Hi, when I try to send notifications to iOS devices the following exception is thrown:

System.InvalidCastException: Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters' to type 'Org.BouncyCastle.Crypto.Parameters.ECPrivateKeyParameters

The code I have implemented is as follows: ` ApnSettings apnSettings = new ApnSettings() { P8PrivateKey = _apnNotificationSettings.P8PrivateKey, P8PrivateKeyId = _apnNotificationSettings.P8PrivateKeyId, TeamId = _apnNotificationSettings.TeamId, AppBundleIdentifier = _apnNotificationSettings.AppBundleIdentifier, ServerType = _apnNotificationSettings.ServerType }; HttpClient httpClient = new HttpClient(); string authorizationKey = string.Format("key={0}", apnSettings.P8PrivateKey); string deviceToken = userDevice.DeviceId;

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorizationKey); httpClient.DefaultRequestHeaders.Accept .Add(new MediaTypeWithQualityHeaderValue("application/json"));

AppleNotificationRequest notification = new AppleNotificationRequest(Guid.NewGuid(), notificationRequest.Body, notificationRequest.Title);

var apn = new ApnSender(apnSettings, httpClient); var apnSendResponse = await apn.SendAsync(notification, deviceToken); if (!apnSendResponse.IsSuccess) res = false; `

If you could help me it would be of great help. Thank you.

arnel-sanchez avatar May 11 '23 08:05 arnel-sanchez

That appears to be a invalid key, I assume the P8 Key is incorrectly formatted

  1. Are you reading the key directly from the file?
  2. Did you copy the p8 file contents to another file that contains your app settings?

If you find yourself in the second point my recommend reading the p8 file directly to avoid any issues, with this I'm also assuming that you have the correct Private Key ID

I also might be wrong but I think you don't need any of those configurations on the HttpClient as CorePush will handle it

Actually the authorization header is set when you call SendAsync

// From "CorePush/Apple/ApnSender.cs" line 85
message.Headers.Authorization = new AuthenticationHeaderValue("bearer", GetJwtToken());

My recommendation would be to try something like this

ApnSettings apnSettings = new ApnSettings() {
    P8PrivateKey = File.ReadAllText("yourp8file.p8"),
    P8PrivateKeyId = _apnNotificationSettings.P8PrivateKeyId,
    TeamId = _apnNotificationSettings.TeamId,
    AppBundleIdentifier = _apnNotificationSettings.AppBundleIdentifier,
    ServerType = _apnNotificationSettings.ServerType
};
HttpClient httpClient = new HttpClient();
string deviceToken = userDevice.DeviceId;

AppleNotificationRequest notification = new AppleNotificationRequest(Guid.NewGuid(), notificationRequest.Body, notificationRequest.Title);

var apn = new ApnSender(apnSettings, httpClient);
var apnSendResponse = await apn.SendAsync(notification, deviceToken);
// [rest of your code]

If the issue persists please try to provide which line is the exception being thrown (line within CorePush that is)

PedroCavaleiro avatar Jan 05 '24 14:01 PedroCavaleiro