简体   繁体   English

Windows Phone 8.1 Silverlight上的推送通知触发器

[英]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. 我正在研究Windows Phone 8.1中可用的“功能”推送通知触发器。 My goal is to make this work with a Windows phone Silverlight 8.1 project. 我的目标是使此功能与Windows Phone Silverlight 8.1项目一起使用。 As far as I know, it should work based on my reading. 据我所知,它应该基于我的阅读。

After 2 days, I'm totally stuck. 2天后,我完全被卡住了。 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. 当我向应用程序发送原始通知时,我的应用程序被取消,并且返回到Windows菜单。

The output : The program '[1852] BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1). 输出:程序“ [1852] BackgroundTASKHOST.EXE”已退出,代码为1(0x1)。 The program '[2712] AgHost.exe' has exited with code 1 (0x1). 程序“ [2712] AgHost.exe”已退出,代码为1(0x1)。

State : 状态:

  • The app receives the notification. 该应用收到通知。 It trigger OnPushNotificationReceived. 它触发OnPushNotificationReceived。 (After this event, the app is canceled) (在此事件之后,该应用将被取消)
  • The Push Notification task is declared and the entry point is defined to Push.Notification . 声明了Push Notification任务,并将入口点定义为Push.Notification
  • I create a Windows Runtime Component for Windows phone 8.1 in order to run the background task. 我为Windows Phone 8.1创建Windows运行时组件,以运行后台任务。
  • 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 : (理论上不是)为了提供信息,我有2条警告令我担心:

  • I have a mismatch in the process architecture in my 2 projects. 我的2个项目的流程体系结构不匹配。 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 我的Windows Phone项目使用“任何CPU”(无法更改)。我的Windows Runetime组件项目使用“ ARM”。如果我使用WinRT的“任何CPU”,则出现错误: / platform:anycpu32bitpreferred只能是与/ t:exe,/ t:winexe和/ 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; 在Visual Studio中,双击该警告(或选择它并按Enter)以解决冲突; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: 否则,将以下绑定重定向添加到应用程序配置文件中的“运行时”节点:

Thanks in advance 提前致谢

I did finally make it works. 我终于做到了。

There was an error in my initial code I forgot to implement IBackgroundTask : 我的初始代码中有一个错误,我忘记了实现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. 希望这会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM