public async Task GetPushToken() { try { const string appServerKey = "BGg3UxXo3J_rH6VrJB2er_F8o7m2ZTGb2jiNm3tmlK4ORxsskX1HIVys5TA8lGkCYC-ur8GwrZMy-v0LZOwazvk"; //IBuffer appServerKeyBuffer = urlB64ToUint8Array(appServerKey).AsBuffer(); IBuffer appServerKeyBuffer = new byte[] { 4, 104, 55, 83, 21, 232, 220, 159, 235, 31, 165, 107, 36, 29, 158, 175, 241, 124, 163, 185, 182, 101, 49, 155, 218, 56, 141, 155, 123, 102, 148, 174, 14, 71, 27, 44, 145, 125, 71, 33, 92, 172, 229, 48, 60, 148, 105, 2, 96, 47, 174, 175, 193, 176, 173, 147, 50, 250, 253, 11, 100, 236, 26, 206, 249 }.AsBuffer(); var channel = await PushNotificationChannelManager.GetDefault().CreateRawPushNotificationChannelWithAlternateKeyForApplicationAsync(appServerKeyBuffer, "mainChannel2"); //var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); //channel.PushNotificationReceived += Channel_PushNotificationReceived; // Foreground event doesn't work TextBoxWebEndpointUrl.Text = channel.Uri; } catch (Exception ex) { await new MessageDialog(ex.ToString()).ShowAsync(); } }
private static async Task <PushSubscription> SubscribeHelper(string applicationServerKey, string channelId) { IBuffer appServerKeyBuffer = UrlB64ToUint8Array(applicationServerKey).AsBuffer(); // No matter what, we always get the channel (we don't do any caching and expiration logic, WNS caches the channel for 24 hours) var channel = await PushNotificationChannelManager.GetDefault().CreateRawPushNotificationChannelWithAlternateKeyForApplicationAsync(appServerKeyBuffer, channelId); var storage = await PushSubscriptionStorage.GetAsync(); var existingSubscriptionInfo = storage.GetSubscriptions(channelId).FirstOrDefault(i => i.ChannelUri == channel.Uri); if (existingSubscriptionInfo != null) { // If the app server key has changed if (existingSubscriptionInfo.AppServerKey != applicationServerKey) { // We need to destroy the WNS channel and create a new one // Close the channel channel.Close(); // Create a new one channel = await PushNotificationChannelManager.GetDefault().CreateRawPushNotificationChannelWithAlternateKeyForApplicationAsync(appServerKeyBuffer, channelId); // And set existing subscription info to null since there's no longer existing info existingSubscriptionInfo = null; } else { // Otherwise, return the existing info return(new PushSubscription() { Endpoint = channel.Uri, Keys = new PushSubscriptionKeys() { Auth = existingSubscriptionInfo.Keys.Auth, P256DH = existingSubscriptionInfo.Keys.P256DH }, Channel = channel, ExpirationTime = channel.ExpirationTime }); } } // Note that we have to store a series of these key pairs... // A developer with an existing channel might call Subscribe again, generating a new channel and key pair, // but they fail to upload the channel/key to their server. Therefore, their server still has the old channel/key, // and pushing to that channel still needs to work, so we need to hold onto the old keys. // We can't delete the old keys until either (1) the expiration time of 30 days for the channel occurs, // or (2) we've received and decrypted a push with the newer key pair, and can therefore throw away the old keys, since // we know at that point that the server successfully received the new channel/key. // Although in case (2), the app developer could still hold onto a previous channel and use it, so the only real truth is the // expiration time. Other than that, any channel that we return MUST keep working. // However, W3C spec states that once a message has been received for a newer subscription, the old ones MUST be deactivated, // so (2) is the correct pattern // Otherwise, we have to create new pairs for this new channel string p256dh; var keyPair = GenerateKeyPair(); p256dh = Uint8ArrayToB64String(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public).PublicKeyData.GetBytes()); var authBytes = CryptographicBuffer.GenerateRandom(16).ToArray(); string auth = Uint8ArrayToB64String(authBytes); await storage.SavePushSubscriptionAsync(channelId, new StoredPushSubscription() { ChannelUri = channel.Uri, Keys = new PushSubscriptionKeys() { Auth = auth, P256DH = p256dh }, AppServerKey = applicationServerKey, P265Private = Uint8ArrayToB64String(PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private).ToAsn1Object().GetDerEncoded()) }); return(new PushSubscription() { Endpoint = channel.Uri, Keys = new PushSubscriptionKeys() { Auth = auth, P256DH = p256dh }, Channel = channel, ExpirationTime = channel.ExpirationTime }); }