我尝试启用我的应用,以便可以使用与Windows Phone模拟器关联的“其他工具”窗口的“通知”标签。
E.g:
我已启用模拟,并重新启动了应用,但未填充AppId
,URI
和其他字段。
如何为推送通知启用应用,以便填充这些字段?
答案 0 :(得分:0)
您需要注册推送通知。当您有推送通知通道时,通知选项卡可以获得推送通知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();
}