订阅已发布应用中的推送通知时出错(但不是调试版)

时间:2017-04-18 03:35:42

标签: c# azure push-notification uwp

我从来没有在调试模式下出现这个错误,这就是为什么在我将我的应用程序发布到商店并尝试崩溃的已发布版本之前我从未注意到它。起初我尝试将调试器附加到商店应用程序的运行副本,但即使加载了完整符号,也没有有用的错误消息。所以我尝试使用Release模式生成应用程序包,并将调试器附加到本地Release版本,当我尝试订阅通知时,我得到了相同的结果,但这次我得到了堆栈跟踪错误,它失败了以下消息

消息

  

“HTTP请求失败。   HTTP详细信息:   状态:400 \ n原因:不好   请求   完整内容:400指定的   资源描述是   invalid..TrackingId:cfbbefdd-1c0e-41A2-bd47-0e0f60c40adb_G13,时间戳:2017年4月18日   上午3:22:48“

栈跟踪

  

StackTrace“at   Microsoft.WindowsAzure.Messaging.RegistrationManager.d__24 1.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\\dd\\ndp\\fxcore\\CoreRT\\src\\System.Private.CoreLib\\src\\System\\Runtime\\ExceptionServices\\ExceptionDispatchInfo.cs:line 61\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in f:\\dd\\ndp\\fxcore\\CoreRT\\src\\System.Private.Threading\\src\\System\\Runtime\\CompilerServices\\TaskAwaiter.cs:line 187\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in f:\\dd\\ndp\\fxcore\\CoreRT\\src\\System.Private.Threading\\src\\System\\Runtime\\CompilerServices\\TaskAwaiter.cs:line 156\r\n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) in f:\\dd\\ndp\\fxcore\\CoreRT\\src\\System.Private.Threading\\src\\System\\Runtime\\CompilerServices\\TaskAwaiter.cs:line 128\r\n at Microsoft.WindowsAzure.Messaging.RegistrationManager.<RegisterAsync>d__0 1.MoveNext(个)\ r \ n   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.CoreLib \ SRC \ SYSTEM \运行\ ExceptionServices \ ExceptionDispatchInfo.cs:线   61 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   187 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   156 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)中   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   128 \ r \ n at   Microsoft.WindowsAzure.Messaging.NotificationHub.d__10.MoveNext(个)\ r \ n   在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.CoreLib \ SRC \ SYSTEM \运行\ ExceptionServices \ ExceptionDispatchInfo.cs:线   61 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   187 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   156 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)中   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   128 \ r \ n at   myApp_Win10.Notifications.d__4.MoveNext()in   d:\ SkyDrive的\源代码\对myApp \ WindowsUniversal \ Notifications.cs:行   53 \ r \ n at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()in   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.CoreLib \ SRC \ SYSTEM \运行\ ExceptionServices \ ExceptionDispatchInfo.cs:线   61 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   187 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   156 \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)中   F:\ DD \ NDP \ fxcore \ CoreRT \ SRC \ System.Private.Threading \ SRC \ SYSTEM \运行\ CompilerServices \ TaskAwaiter.cs:线   128 \ r \ n at myApp_Win10.Views.Settings.d__4.MoveNext()   在   d:\ SkyDrive的\源代码\对myApp \ WindowsUniversal \查看\ Settings.xaml.cs:行   72“字符串

我尝试订阅的代码

   private async void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            this.progress.IsActive = true;
            string category = (string)App.localSettings.Values["notificationCategory"];

            List<string> categories = new List<string>();
            categories.Add((category).Replace(" ", "_"));
            foreach (string s in categories)
                System.Diagnostics.Debug.WriteLine("Categories: " + s);
            //subscribe to new channel Uri

            Notifications notifications = new Notifications("myHub", "MyEndpoint");
            var result = await notifications.SubscribeToCategories(categories);
            this.progress.IsActive = false;
            if (result != null)
            {

                MessageDialog md = new MessageDialog("Successfully subscribed to notifications for " + category , "Subscribed!");

                await md.ShowAsync();


            }
            else
            {
                MessageDialog md = new MessageDialog("Subscribe to notifications failedd. Please try again later.  If the problem persists please send feedback to the developer. Thank you.", "Failed!");

                await md.ShowAsync();
            }

        }catch(Exception ex)
        {

        }
    }

我正在使用 DefaultListenSharedAccessSignature ,我尝试重新生成主连接字符串,但结果相同。

编辑 - 这是我的通知类

public class Notifications
{

    private NotificationHub hub;

    public Notifications(string hubName, string listenConnectionString)
    {
        hub = new NotificationHub(hubName, listenConnectionString);
    }

    public async Task<Registration> StoreCategoriesAndSubscribe(IEnumerable<string> categories)
    {
        return await SubscribeToCategories(categories);
    }

    public IEnumerable<string> RetrieveCategories()
    {
        var category = (string)App.localSettings.Values["notificationCategory"];
        var categories = new List<string>();
        categories.Add(category);

        return categories;
    }

    public async Task<Registration> SubscribeToCategories(IEnumerable<string> categories = null)
    {
        var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

        if (categories == null)
        {
            categories = RetrieveCategories();
        }

        // Using a template registration to support notifications across platforms.
        // Any template notifications that contain messageParam and a corresponding tag expression
        // will be delivered for this registration.

        const string templateBodyWNS = "<toast><visual><binding template=\"ToastText01\"><text id=\"1\">$(toastMessage)</text></binding></visual></toast>";

        return await hub.RegisterTemplateAsync(channel.Uri, templateBodyWNS, "",
                categories);
    }

}

1 个答案:

答案 0 :(得分:0)

根据您提到的例外情况,尝试注册通知时参数不正确。

  

HTTP请求失败。 HTTP详细信息:状态:400 \ nReason:错误请求完整内容:400指定的资源描述无效。

请尝试使用fiddler获取详细错误信息。就我而言,它在发布和调试模式下都有效。有关如何使用通知中心进行通知的更多详细信息,请参阅document

注意:请在发布应用之前删除所有断点。 如果可能,请分享您的注册码。

enter image description here

public class Notifications
    {
        private NotificationHub hub;

        public Notifications(string hubName, string listenConnectionString)
        {
            hub = new NotificationHub(hubName, listenConnectionString);
        }
        public async Task<Registration> StoreCategoriesAndSubscribe(IEnumerable<string> categories)
        {
            ApplicationData.Current.LocalSettings.Values["categories"] = string.Join(",", categories);
            return await SubscribeToCategories(categories);
        }
        public async Task<Registration> SubscribeToCategories(IEnumerable<string> categories = null)
        {
            var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

            if (categories == null)
            {
                categories = RetrieveCategories();
            }

            // Using a template registration to support notifications across platforms.
            // Any template notifications that contain messageParam and a corresponding tag expression
            // will be delivered for this registration.

            const string templateBodyWNS = "<toast><visual><binding template=\"ToastText01\"><text id=\"1\">$(messageParam)</text></binding></visual></toast>";

            return await hub.RegisterTemplateAsync(channel.Uri, templateBodyWNS, "simpleWNSTemplateExample",
                    categories);
        }
        public IEnumerable<string> RetrieveCategories()
        {
            var categories = (string)ApplicationData.Current.LocalSettings.Values["categories"];
            return categories != null ? categories.Split(',') : new string[0];
        }