Skip to content

Instantly share code, notes, and snippets.

@tophyr
Created July 30, 2012 23:01
Show Gist options
  • Save tophyr/3211607 to your computer and use it in GitHub Desktop.
Save tophyr/3211607 to your computer and use it in GitHub Desktop.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Failed to get token, error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Received remote notification: %@", userInfo);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Security.Authentication;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace Tinkering
{
class Program
{
// Get the APNS cert
private static X509Certificate getServerCert()
{
// Open the cert store on the Local Machine
X509Store store = new X509Store(StoreLocation.LocalMachine);
if (store != null)
{
// Store exists, so open it and search through the certs for the Apple cert
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certs = store.Certificates;
if (certs.Count > 0)
{
int i;
for (i = 0; i < certs.Count; i++)
{
X509Certificate2 cert = certs[i];
if (cert.FriendlyName.Contains("Apple Development IOS Push Services"))
{
// Cert found, so return it
return certs[i];
}
}
}
}
return null;
}
private static Stream s_PushConnection;
private static Stream PushConnection
{
get
{
if (s_PushConnection == null || !s_PushConnection.CanWrite)
{
TcpClient client = new TcpClient("gateway.sandbox.push.apple.com", 2195);
SslStream ssl = new SslStream(client.GetStream(), false, null);
try
{
X509Certificate cert = getServerCert();
ssl.AuthenticateAsClient("gateway.sandbox.push.apple.com",
new X509CertificateCollection(new X509Certificate[] { cert }),
System.Security.Authentication.SslProtocols.Default,
true);
}
catch (AuthenticationException e)
{
client.Close();
ssl = null;
// need to log this
throw e;
}
s_PushConnection = ssl;
}
return s_PushConnection;
}
}
private static byte[] HexStringToByteArray(string hex)
{
if ((hex.Length & 1) == 1)
throw new Exception("The binary key cannot have an odd number of digits");
byte[] arr = new byte[hex.Length >> 1];
for (int i = 0; i < hex.Length; i += 2)
{
arr[i >> 1] = (byte)((GetHexVal(hex[i]) << 4) + (GetHexVal(hex[i + 1])));
}
return arr;
}
private static int GetHexVal(char hex)
{
int val = (int)hex;
return val - (val < 58 ? 48 : (val < 97 ? 55 : 87));
}
static void Main(string[] args)
{
Guid envId = new Guid();
string payload = String.Format("{{'aps':{{'alert':'This is a push notification'}},'envId':'{0}'}}", envId);
BinaryWriter bw = new BinaryWriter(PushConnection);
bw.Write((byte)0);
//bw.Write((byte)1);
//// identifier
//bw.Write((uint)0xADADADAD);
//uint expiry = (uint)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds + 86400;
//bw.Write((byte)(expiry >> 24));
//bw.Write((byte)(expiry >> 16));
//bw.Write((byte)(expiry >> 8));
//bw.Write((byte)expiry);
byte[] DeviceId = HexStringToByteArray("dc1a93735d2b6a953a2f98ac886b8eee38a1c9498387565ccca5cc944c73a7e6");
bw.Write((byte)(DeviceId.Length >> 8));
bw.Write((byte)DeviceId.Length);
bw.Write(DeviceId);
bw.Write((byte)(payload.Length >> 8));
bw.Write((byte)payload.Length);
bw.Write(payload.ToCharArray(), 0, payload.Length);
bw.Flush();
//int i;
//while ((i = PushConnection.ReadByte()) != -1)
// Console.WriteLine(i);
}
}
}
My token is: <dc1a9373 5d2b6a95 3a2f98ac 886b8eee 38a1c949 8387565c cca5cc94 4c73a7e6>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment