Reputation: 7278
I would like to send a test PUSH message to an iOS device. I am provided with the device token and one .pem
file as certificate. But I cannot seem to find a way to create a new ApplePushChannelSettings
object using this .pem
file. The examples on the web all use a .p12
file that come with a password.
//Create our push services broker
var push = new PushBroker();
//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes("ApnsSandboxCert.p12"));
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "pwd"));
push.QueueNotification(new AppleNotification()
.ForDeviceToken("DEVICE TOKEN HERE")
.WithAlert("Hello World!")
.WithBadge(7)
.WithSound("sound.caf"));
Should I ask for a .p12
certificate for the target app or is there a way to use a .pem
file with PushSharp?
Upvotes: 0
Views: 1256
Reputation: 1403
Actually, you can, I've managed to do it after a long time of researching. First, get this: https://github.com/jrnker/CSharp-easy-RSA-PEM. It contains a class called Crypto and another called Helpers that I've used for sending push with .pem files.
X509Certificate2 GetCertificate(string pemFilePath, string pemFilePassword)
{
var pemString = File.ReadAllText(pemFilePath);
var certificateString = readSectionFromPem(pemString, "CERTIFICATE");
var keyString = readSectionFromPem(pemString, "RSA PRIVATE KEY");
var certificateBuffer = Helpers.GetBytesFromPEM(certificateString, PEMtypes.PEM_X509);
X509Certificate2 certificate = new X509Certificate2(certificateBuffer);
RSACryptoServiceProvider provider = Crypto.DecodeRsaPrivateKey(keyString, pemFilePassword);
certificate.PrivateKey = provider;
return certificate;
}
string readSectionFromPem(string pem, string section)
{
string header = String.Format("-----BEGIN {0}-----", section);
string footer = String.Format("-----END {0}-----", section);
int start = pem.IndexOf(header);
int end = pem.IndexOf(footer, start) + footer.Length;
return pem.Substring(start, end - start);
}
After you obtain the X509Certificate2 instance, you can instantiate your ApnsConfiguration and ApnsBroker instances with the following code
ApnsConfiguration config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, GetCertificate(certificatePath, certificatePass));
ApnsServiceBroker apnsBroker = new ApnsServiceBroker(config);
Upvotes: 0
Reputation: 7876
From my experience, all Push notifications services ask for a .p12
certificate. You should try that.
EDIT:
After a quick search, I think that you don't have a choice. .p12
it is.
Upvotes: 2