Come posso creare un'app Push Abilitata in Windows Phone 8.1?
-
21-12-2019 - |
Domanda
Sto cercando di abilitare la mia app in modo che io possa utilizzare la scheda Notifiche delle finestre degli strumenti aggiuntive associate all'emulatore di Windows Phone.
E.G.:
Ho abilitato la simulazione e rilancia l'app ma il AppId
, URI
e altri campi non vengono popolati.
Come posso abilitare l'app per le notifiche push in modo che questi campi saranno popolati?
Soluzione
È necessario registrarsi per le notifiche push.Quando si dispone di un canale di notifiche push, la scheda Notifiche può quindi ottenere le notifiche push uri
public IAsyncOperation<ChannelAndWebResponse> OpenChannelAndUploadAsync(String url)
{
IAsyncOperation<PushNotificationChannel> channelOperation = PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
return ExecuteChannelOperation(channelOperation, url, MAIN_APP_TILE_KEY, true);
}
public IAsyncOperation<ChannelAndWebResponse> OpenChannelAndUploadAsync(String url, String inputItemId, bool isPrimaryTile)
{
IAsyncOperation<PushNotificationChannel> channelOperation;
if (isPrimaryTile)
{
channelOperation = PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(inputItemId);
}
else
{
channelOperation = PushNotificationChannelManager.CreatePushNotificationChannelForSecondaryTileAsync(inputItemId);
}
return ExecuteChannelOperation(channelOperation, url, inputItemId, isPrimaryTile);
}
private IAsyncOperation<ChannelAndWebResponse> ExecuteChannelOperation(IAsyncOperation<PushNotificationChannel> channelOperation, String url, String itemId, bool isPrimaryTile)
{
return channelOperation.AsTask().ContinueWith<ChannelAndWebResponse>(
(Task<PushNotificationChannel> channelTask) =>
{
PushNotificationChannel newChannel = channelTask.Result;
String webResponse = "URI already uploaded";
// Upload the channel URI if the client hasn't recorded sending the same uri to the server
UrlData dataForItem = TryGetUrlData(itemId);
if (dataForItem == null || newChannel.Uri != dataForItem.ChannelUri)
{
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
byte[] channelUriInBytes = Encoding.UTF8.GetBytes("ChannelUri=" + WebUtility.UrlEncode(newChannel.Uri) + "&ItemId=" + WebUtility.UrlEncode(itemId));
Task<Stream> requestTask = webRequest.GetRequestStreamAsync();
using (Stream requestStream = requestTask.Result)
{
requestStream.Write(channelUriInBytes, 0, channelUriInBytes.Length);
}
Task<WebResponse> responseTask = webRequest.GetResponseAsync();
using (StreamReader requestReader = new StreamReader(responseTask.Result.GetResponseStream()))
{
webResponse = requestReader.ReadToEnd();
}
}
// Only update the data on the client if uploading the channel URI succeeds.
// If it fails, you may considered setting another AC task, trying again, etc.
// OpenChannelAndUploadAsync will throw an exception if upload fails
//https://db3.notify.windows.com/?token=AgYAAABRl%2fdFlmxVkJPDROr5LRwp4tRW0Hcp006ODyaAkBppfiPDsAW%2fNRqsaIc0UFw3DMA15C%2btvGwtkreUZrBLs9vxOxCWL0fYw%2ft2HDFuTDM4szMxFWGom6B3kkGMiPa4C1o%3d
UpdateUrl(url, newChannel.Uri, itemId, isPrimaryTile);
return new ChannelAndWebResponse { Channel = newChannel, WebResponse = webResponse };
}).AsAsyncOperation();
}
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow