Search code examples
c#silverlightwindows-phone-8windows-phone-8.1windows-phone

Push Notification Trigger on Windows Phone 8.1 Silverlight


I'm working on the "feature" the push notification trigger available in Windows phone 8.1. My goal is to make this work with a Windows phone Silverlight 8.1 project. As far as I know, it should work based on my reading.

After 2 days, I'm totally stuck. No matter what i'm trying. When I send a raw notification to my app, my app is canceled and I'm back to the windows menu.

The output : The program '[1852] BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1). The program '[2712] AgHost.exe' has exited with code 1 (0x1).

State :

  • The app receives the notification. It trigger OnPushNotificationReceived. (After this event, the app is canceled)
  • The Push Notification task is declared and the entry point is defined to Push.Notification.
  • I create a Windows Runtime Component for Windows phone 8.1 in order to run the background task.
  • The background task is well registered.

private async void RegisterBackgroundTask()
{
    UnregisterBackgroundTask(taskName);
    BackgroundAccessStatus backgroundStatus = await BackgroundExecutionManager.RequestAccessAsync();
    if (backgroundStatus != BackgroundAccessStatus.Denied && backgroundStatus != BackgroundAccessStatus.Unspecified)
    {
        try
        {
            BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
            taskBuilder.Name = taskName;
            PushNotificationTrigger trigger = new PushNotificationTrigger();
            taskBuilder.SetTrigger(trigger);
            taskBuilder.TaskEntryPoint = "Push.Notification";
            BackgroundTaskRegistration task = taskBuilder.Register();
        }
        catch (Exception ex)
        { }
    }
}
  • The background task :

public sealed class Notification
{
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Background starting...");
        Debug.WriteLine("Background completed!");
    }
}

I have no clue about what I'm doing wrong. Is there someone who make it works ? (Not in theory) For information, I have 2 warnings that worrying me :

  • I have a mismatch in the process architecture in my 2 projects. My windows phone project use "Any CPU" (can"t change that). My windows runetime component project use "ARM". If I use "Any CPU" wor my WinRT, i got an errror : /platform:anycpu32bitpreferred can only be used with /t:exe, /t:winexe and /t:appcontainerexe
  • There is a conflict on a dependent assembly of the 2 projects. Seems to be

Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file:

Thanks in advance


Solution

  • I did finally make it works.

    There was an error in my initial code I forgot to implement IBackgroundTask :

    public sealed class Notification : IBackgroundTask
    {
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background starting...");
            Debug.WriteLine("Background completed!");
        }
    }
    

    And I had to make my Task async...

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
        // perform your async task and then call deferral  complete
        await Test();
        deferral.Complete();
    }
    
    private static async Task Test()
    {
        //TODO with an await
    }
    

    Hope this'll help someone.