locked
[C#]Receive raw notification when app closed and open RRS feed

  • Question

  • Hello, is there a way to receive notifications:

    1. When app is open, using the PushNotificationReceived event

    2. When app is closed, using the backgroundtask

    Otherwise, when the app receives notification when it is running, I have error related to threading. I need any solution to get rid of this.

    public sealed class PushReceiveTask : IBackgroundTask
        {
            BackgroundTaskDeferral _deferral = null;
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                try
                {
                    _deferral = taskInstance.GetDeferral();
                    // Get the background task details 
                    ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
                    string taskName = taskInstance.Task.Name;
    
                    //var notificationCount = ApplicationData.Current.LocalSettings.Values["ntf_cnt"] as string;
    
                    //if (string.IsNullOrEmpty(notificationCount))
                    //{
                    //    ApplicationData.Current.LocalSettings.Values["ntf_cnt"] = "1";
                    //}
                    //else
                    //{
                    //    var number = Convert.ToInt32(notificationCount);
                    //    number++;
                    //    ApplicationData.Current.LocalSettings.Values["ntf_cnt"] = number.ToString();
                    //}
    
                    // Store the content received from the notification so it can be retrieved from the UI. 
                    RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
                    settings.Values[taskName] = notification.Content;
    
                    var doc = new XmlDocument();
                    doc.LoadXml(notification.Content.Trim());
    
    
                    var toast = new ToastNotification(doc);
    
                    ToastNotificationManager.CreateToastNotifier().Show(toast);
    
                    _deferral.Complete();
                }
                catch (Exception)
                {
                    //ignored
                }
    
            }
        }

    The above code is background task

    public void RegisterBackgroundTask() {

    var access = await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder(); PushNotificationTrigger trigger = new PushNotificationTrigger();

    builder.SetTrigger(trigger); builder.Name = "PushReceiveTask"; builder.TaskEntryPoint = "BackgroundWorker.PushReceiveTask"; builder.Register(); }


    The above code is for registering the backgroun task.

    And finally I create the channel as seen below:

    Channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

    Channel.PushNotificationReceived += Channel_PushNotificationReceived; RegisterBackgroundTask();



    • Edited by Krunal Parekh Monday, February 1, 2016 4:46 AM subject
    Saturday, January 30, 2016 11:41 AM

Answers

  • Hello BulutFatih,

    You can create and register a background task function for your Windows Store app. This code then runs in response to the receipt of a raw notification, which gives your app functionality when it is not in the foreground.

    Quickstart: Creating and registering a raw notification background task (XAML)

    See http://code.msdn.microsoft.com/windowsapps/Raw-notifications-sample-3bc28c5d for a sample that does this.

    >>I have error related to threading. I need any solution to get rid of this.

    What kind of error are you having can you please share the error details?

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Friday, February 5, 2016 9:30 AM
    • Marked as answer by Krunal Parekh Monday, February 15, 2016 9:47 AM
    Monday, February 1, 2016 4:57 AM

All replies

  • Hello, I am new and  have a problem with handling raw notifications that sometimes I can get and show the notifications but most of the times it gives me the error:

    An unhandled exception of type 'System.ExecutionEngineException' occurred in Unknown Module.

    And when I check them with eventviwer, I see the error:

    The background task having the entrypoint BackgroundWorker.PushReceiveTask and name PushReceiveTask could not be actived.

    Below is code:

    public sealed class PushReceiveTask : IBackgroundTask
        {
            BackgroundTaskDeferral _deferral = null;
            public void Run(IBackgroundTaskInstance taskInstance)
            {
                try
                {
                    _deferral = taskInstance.GetDeferral();
                    // Get the background task details 
                    ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
                    string taskName = taskInstance.Task.Name;
    
                    //var notificationCount = ApplicationData.Current.LocalSettings.Values["ntf_cnt"] as string;
    
                    //if (string.IsNullOrEmpty(notificationCount))
                    //{
                    //    ApplicationData.Current.LocalSettings.Values["ntf_cnt"] = "1";
                    //}
                    //else
                    //{
                    //    var number = Convert.ToInt32(notificationCount);
                    //    number++;
                    //    ApplicationData.Current.LocalSettings.Values["ntf_cnt"] = number.ToString();
                    //}
    
                    // Store the content received from the notification so it can be retrieved from the UI. 
                    RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
                    settings.Values[taskName] = notification.Content;
    
                    var doc = new XmlDocument();
                    doc.LoadXml(notification.Content.Trim());
    
    
                    var toast = new ToastNotification(doc);
    
                    ToastNotificationManager.CreateToastNotifier().Show(toast);
    
                    _deferral.Complete();
                }
                catch (Exception)
                {
                    //ignored
                }
    
            }
        }

    The above code is background task

    public void RegisterBackgroundTask() {

    var access = await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder(); PushNotificationTrigger trigger = new PushNotificationTrigger();

    builder.SetTrigger(trigger); builder.Name = "PushReceiveTask"; builder.TaskEntryPoint = "BackgroundWorker.PushReceiveTask"; builder.Register(); }


    The above code is for registering the backgroun task.

    And finally I create the channel as seen below:

    Channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    RegisterBackgroundTask();

    I want to ask you what is the problem and how can I solve this?

    • Merged by Krunal Parekh Tuesday, February 2, 2016 2:32 AM Same case
    Saturday, January 30, 2016 10:57 AM
  • Is this during a debug session?

    http://pauliom.wordpress.com

    Saturday, January 30, 2016 11:07 AM
  • Yes, I havent released the app yet. if you ask about debugging with breakpoints, it problem occurs when not only debugging with breakpoints but also running without breakpoints.
    Saturday, January 30, 2016 12:08 PM
  • Yes, ExecutionEngineException seems to be proof that the debugger isn't 100%. Use logging to files to diagnose your issues. But yes that problem is VERY frustrating if anyone from MS is reading this PLEASE fix that.

    http://pauliom.wordpress.com


    • Edited by pkr2000 Sunday, January 31, 2016 9:03 AM
    Sunday, January 31, 2016 9:03 AM
  • Hello BulutFatih,

    You can create and register a background task function for your Windows Store app. This code then runs in response to the receipt of a raw notification, which gives your app functionality when it is not in the foreground.

    Quickstart: Creating and registering a raw notification background task (XAML)

    See http://code.msdn.microsoft.com/windowsapps/Raw-notifications-sample-3bc28c5d for a sample that does this.

    >>I have error related to threading. I need any solution to get rid of this.

    What kind of error are you having can you please share the error details?

    With Regards,

    Krunal Parekh


    Thanks MSDN Community Support Please remember to Mark as Answer the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Krunal Parekh Friday, February 5, 2016 9:30 AM
    • Marked as answer by Krunal Parekh Monday, February 15, 2016 9:47 AM
    Monday, February 1, 2016 4:57 AM
  • This looks like a duplicate post of

    https://social.msdn.microsoft.com/Forums/windowsapps/en-US/58e112f3-2c7d-4b88-878b-d9eeab5e4358/backgroundtask-cannot-be-started-most-of-the-times-when-raw-notification-received?forum=wpdevelop#b825e5d0-fbcc-4994-822f-651b058b706b

    Have you moved on from there?


    http://pauliom.wordpress.com

    Monday, February 1, 2016 7:22 AM