2017-04-07 5 views
0

Raw Pushnotificationトリガーによってトリガーされるバックグラウンドタスクを持つWindows 10 UWPアプリケーションを開発しようとしています。テストとして、私はRaw Notificationが実際に正しくプッシュしているかどうかを確認するためのハンドラもアプリケーションに持っています。ここでは、コードスニペットは、次のとおりです。UWP PushNotificationTriggerはバックグラウンドタスクを起動しません

PushNotificationChannel _channel = null; 

    private async void AppInit() 
    { 
     _channel = await Common.WNSHelper.GetChannelAsync(); 
     if (_channel != null) 
     { 
      _channel.PushNotificationReceived += _channel_PushNotificationReceived; 
     } 
     await InstallPositionBackgroundTask(); 
     } 

    private async void _channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args) 
    { 
     if (args.NotificationType == PushNotificationType.Raw) 
     { 
      Common.GeneralHelper.SendToast("At App PositionBackgroundTask"); 
     } 
    } 

    private async Task InstallPositionBackgroundTask() 
    { 
     var taskName = "PositionBackgroundTask"; 

     foreach (var tsk in BackgroundTaskRegistration.AllTasks) 
     { 
      if (tsk.Value.Name == taskName) 
      { 
       tsk.Value.Unregister(true); 
      } 
     } 

     var cost = BackgroundWorkCost.CurrentBackgroundWorkCost; 
     if (cost == BackgroundWorkCostValue.High) 
     { 
      return; 
     } 

     var allowed = await BackgroundExecutionManager.RequestAccessAsync(); 
     if (allowed == BackgroundAccessStatus.AllowedSubjectToSystemPolicy 
      || allowed == BackgroundAccessStatus.AlwaysAllowed) 
     { 
      var builder = new BackgroundTaskBuilder(); 

      builder.Name = nameof(PositionBackgroundTask.PositionBackgroundTask); 
      builder.CancelOnConditionLoss = false; 
      builder.TaskEntryPoint = typeof(PositionBackgroundTask.PositionBackgroundTask).FullName; 
      builder.SetTrigger(new PushNotificationTrigger()); 
      BackgroundTaskRegistration task = builder.Register(); 
      task.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted); 

      Common.GeneralHelper.SendToast("Position Task Installed. " + System.DateTime.Now.ToString()); 
     } 
    } 
    private async void button_Click(object sender, RoutedEventArgs e) 
    { 
     // send it 
     var response = await Common.WNSHelper.PushRawAsync<Common.RawRequest>(_channel.Uri, new RawRequest() { RequestType = RawRequestType.RequestPosition, FromDeviceId = _allDevices[0].D_Id }, null); 
    } 

はここでバックグラウンドタスク

namespace PositionBackgroundTask 

{ 
    public sealed class PositionBackgroundTask 
    { 

     BackgroundTaskDeferral _deferral; 
     public async void Run(IBackgroundTaskInstance taskInstance) 
     { 
      Common.GeneralHelper.SendToast("At PositionBackgroundTask"); 

      var cancel = new System.Threading.CancellationTokenSource(); 
      taskInstance.Canceled += (s, e) => 
      { 
       cancel.Cancel(); 
       cancel.Dispose(); 
      }; 

      _deferral = taskInstance.GetDeferral(); 
      try 
      { 
       RawNotification notification = (RawNotification)taskInstance.TriggerDetails; 
       //: 
       //: 
       //: 
      } 
      finally 
      { 
       _deferral.Complete(); 
      } 
     } 
    } 
} 

はここでマニフェスト

<Extension Category="windows.backgroundTasks" EntryPoint="PositionBackgroundTask.PositionBackgroundTask"> 
    <BackgroundTasks> 
    <Task Type="pushNotification" /> 
    </BackgroundTasks> 
</Extension> 

だ。ここ生の通知に

public static async Task<HttpResponseMessage> PushRawAsync<T>(string channelUri, RawRequest request, T rawData) 
{ 
    // Get App authorization 
    string appDataSecret = MYAPPSECRET; 
    Uri requestUri = new Uri(@"https://login.live.com/accesstoken.srf"); 

    var client = new System.Net.Http.HttpClient(); 
    System.Net.Http.HttpResponseMessage response = await client.PostAsync(requestUri, new StringContent(appDataSecret, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded")); 
    string responJsonText = await response.Content.ReadAsStringAsync(); 

    WMNToken token = JsonConvert.DeserializeObject<WMNToken>(responJsonText); 

    string requestStr = JsonConvert.SerializeObject(request) + ";" + JsonConvert.SerializeObject(rawData); 
    var content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(requestStr))); 

    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(token.Token_type, token.Access_token); 
    client.DefaultRequestHeaders.Add("X-WNS-Type", "wns/raw"); 

    response = await client.PostAsync(channelUri, content); 
    return response; 
} 

送信するためのコードですですアプリ定義yは通知を受け取ったが、バックグラウンドタスクは決して受け取らなかった。私は何を間違えたのですか?

詳細: はプッシュ通知がボックスに当たっているように見えますが、イベントログ

Activation of the app 45737MyName.TestPushNotificationpartofemail_4rhf8erfmecqa!App for the Windows.BackgroundTasks contract failed with error: No such interface supported. 

でこのエラーを持って、この

ようにする必要があり、それは私がIBackgroundTask のサブクラスを作成しませんでしたが判明
public sealed class PositionBackgroundTask : IBackgroundTask 
+0

通知なしでバックグラウンドタスクを実行できますか?つまり、コード/ボタンのクリックで手動でトリガーできますか? – Laith

+0

私が読んだところから、プッシュ通知はVisual Studio内では起動できません。私は実際にTimeTriggerイベントのバックグラウンドタスクを持っており、Visual Studio内でそれをテストすることができました。実際にプッシュ通知バックグラウンドタスクを実行する方法はありますか? – dreamfly

+0

私はあなたのバックグラウンドタスクコードが別のトリガーでトリガされたことを確認したかっただけです。 – Laith

答えて

0

バックグラウンドタスクはIBackgroundTaskのサブクラスである必要があります

公開密封クラスPosition BackgroundTask:IBackgroundTask

関連する問題