如何在Windows Phone8.1中创建启用推送的应用?
-
21-12-2019 - |
题
我正在尝试启用我的应用程序,以便我可以使用与Windows Phone模拟器关联的其他工具窗口的通知选项卡。
例如。:
我已经启用了模拟,并重新启动了应用程序,但 AppId
, URI
而其他字段没有被填充。
如何为推送通知启用应用程序,以便填充这些字段?
解决方案
您需要注册推送通知。当您有推送通知通道时,通知选项卡可以获取推送通知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();
}
不隶属于 StackOverflow