locked
Background Task (Raw notifications) stops working after x hours

    Question

  • Hi!

    I've built an Windows Store app (using MVVM pattern) that is listening to raw notifications and everything works fine the first couple of hours (yes i have unchecked the quiet hours) but when I come back the day after it stops receiving and closing/resuming the app doesen't solve the problem. Since I have to test it over a day or so I've tried a couple of things and the code at this given moment looks like this:

    In MainPage:

    private void RegisterBackgroundTask() { BackgroundTaskBuilder taskbuilder = new BackgroundTaskBuilder(); PushNotificationTrigger trigger = new PushNotificationTrigger(); taskbuilder.TaskEntryPoint = ENTRY_POINT; taskbuilder.Name = TASK_NAME; taskbuilder.SetTrigger(trigger); try { IBackgroundTaskRegistration task = taskbuilder.Register(); taskbuilder.CancelOnConditionLoss = true; task.Completed += BackgroundTaskCompleted; } catch (Exception ex) {

    Debug.WriteLine("Registration error: " + ex.Message); UnregisterBackgroundTask(); } }


    The BackgroundTask class:

           void IBackgroundTask.Run(IBackgroundTaskInstance taskInstance)
            {
                ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
                string taskName = taskInstance.Task.Name;
    
                Debug.WriteLine("Background " + taskName + " starting...");
                RawNotification notification = (RawNotification)taskInstance.TriggerDetails;
                settings.Values[taskName] = notification.Content;
    
                Debug.WriteLine("Background " + taskName + " completed!");
            }

    And in a ViewModel I request a channel if it hasen't expired (i've tried to request a new one all the time but with the same result)

    It's basically CurrentChannel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); then inserting it to table (somewhat like the examples provided from you) and an UpdateListener method:

     private void UpdateListener(bool add)
            {
                try
                {
                    if (CurrentChannel != null && CurrentChannel.Uri != null)
                    {
                        if (add && !eventAdded)
                        {
                            CurrentChannel.PushNotificationReceived -= OnPushNotificationReceived;
                            CurrentChannel.PushNotificationReceived += OnPushNotificationReceived;
                            eventAdded = true;
                            Debug.WriteLine("Created");
                        }
                        else if (!add && eventAdded)
                        {
                            CurrentChannel.PushNotificationReceived -= OnPushNotificationReceived;
                            eventAdded = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandleException(ex);
                }
            }

    After I install the App it works fine from start and closing and/or resuming works aswell. The day after it stops working (does not receive any notifications and it is nowhere near the limit of x amount of calls).

    Any ideas?

    Thanks,

    Mikael



    Tuesday, December 16, 2014 10:01 AM

All replies

  • Is this occurring on phone or desktop? Are you sure that the notification is being pushed through?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, December 16, 2014 8:03 PM
    Moderator
  • Hi!

    I'll look deeper into the service (that I for some reason diden't mention).

    Thanks!

    Wednesday, December 17, 2014 12:13 PM
  • Hi again!

    It is for desktop only and no, it did not push through. I'm clueless atm but what I know is this: The service has a method that sends a subscription using the PushSubscription class. It then stores the subscriptionId in a table along with an e-mail adress to compare to later on (the push listener). How often do we have to push for a new subscription?

    Could the problem be that the subscription ends and isen't updated on the server?

    Thursday, December 18, 2014 8:56 AM
  • I'm feeling dumb... I can't find the "PushSubscription" class in the APIs. What am I missing?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, December 18, 2014 3:47 PM
    Moderator