我正在嘗試爲Windows Phone 8.1 Silverlight應用程序註冊後臺任務,但一直未能成功實現此目的。從我在網上看到的樣本中,我沒有看到我做錯了什麼。無論如何,下面是我配置的設置和代碼片段的一些快照。如果有人能夠請我用我失蹤的東西來啓迪我,我將不勝感激!xamarin表單win電話8.1後臺任務推送通知不註冊
運通電話工程(WMAppManifest.xml)
運通電話工程(Package.appxmanifest)
後臺任務項目類型
WinPhone通知管理器
該類繼承其用於調用利用該通知轂針對寄存器的事件的接口依賴於PCL
using Microsoft.Phone.Notification;
using Microsoft.WindowsAzure.Messaging;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using MyApp.Common.Models;
using MyApp.Interfaces;
using MyApp.Utilities;
using MyApp.WinPhone;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
[assembly: Xamarin.Forms.Dependency(typeof(PushNotificationManager))]
namespace MyApp.WinPhone
{
public class PushNotificationManager : IPushNotificationManager
{
private NotificationHub hub;
private const string TASK_NAME = "NotificationTask";
private const string TASK_ENTRY_POINT = "MyApp.Background.NotificationTask";
public PushNotificationManager() { }
public async Task RegisterDevice()
{
try
{
var channel = HttpNotificationChannel.Find("NotificationChannel");
if (channel == null)
{
channel = new HttpNotificationChannel("NotificationChannel");
channel.Open();
channel.BindToShellToast();
}
// Register device
channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(NotificationChannel_ChannelUriUpdated);
// Register for this notification only if you need to receive the notifications while your application is running.
channel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(NotificationChannel_ShellToastNotificationReceived);
channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(NotificationChannel_HttpNotificationReceived);
// Clean out the background task
await UnregisterBackgroundTask();
await RegisterBackgroundTask();
}
catch (Exception ex)
{
throw ex;
}
}
public async Task UnregisterDevice()
{
var channel = HttpNotificationChannel.Find("NotificationChannel");
if (channel != null)
{
var hub = new NotificationHub(Utilities.Constants.ApplicationHubName, Utilities.Constants.ApplicationConnectionString);
await hub.UnregisterAllAsync(channel.ChannelUri.ToString());
channel.Close();
// Clean out the background task
await UnregisterBackgroundTask();
}
}
protected void NotificationChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
RegisterWinDevice(e.ChannelUri.ToString());
}
protected void NotificationChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
try
{
//Get title and message
CreateNotification(title, message);
}
catch(Exception ex)
{
throw ex;
}
}
protected void NotificationChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
try
{
//Get title and message
CreateNotification(title, message);
}
catch(Exception ex)
{
throw ex;
}
}
private async Task RegisterWinDevice(string channelUri)
{
try
{
hub = new NotificationHub(Constants.ApplicationHubName, Constants.ApplicationConnectionString);
var tags = new List<string>() { };
User user = LocalStorage.GetUserInfo();
tags.Add(user.Id.ToString());
var result = await hub.RegisterNativeAsync(channelUri, tags.ToArray());
}
catch (Exception ex)
{
throw ex;
}
}
private void CreateNotification(string title, string message)
{
//Show toast notification
}
private async Task RegisterBackgroundTask()
{
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
PushNotificationTrigger trigger = new PushNotificationTrigger();
await BackgroundExecutionManager.RequestAccessAsync();
// Background tasks must live in separate DLL, and be included in the package manifest
// Also, make sure that your main application project includes a reference to this DLL
taskBuilder.TaskEntryPoint = TASK_ENTRY_POINT;
taskBuilder.Name = TASK_NAME;
taskBuilder.SetTrigger(trigger);
try
{
BackgroundTaskRegistration task = taskBuilder.Register();
}
catch (Exception ex)
{
await UnregisterBackgroundTask();
}
}
private async Task<bool> UnregisterBackgroundTask()
{
foreach (var iter in BackgroundTaskRegistration.AllTasks)
{
IBackgroundTaskRegistration task = iter.Value;
if (task.Name == TASK_NAME)
{
task.Unregister(true);
return true;
}
}
return false;
}
}
}
後臺任務項目(NotificationTask)
using System;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.Networking.PushNotifications;
using Windows.UI.Notifications;
namespace MyApp.Background
{
public sealed class NotificationTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
//Get Raw Notification and show Toast Notification
}
}
}
做更多的事情需要注意:手機寄存器正確地Azures通知樞紐和有沒有正在運行的應用程序或當我發送一推時引發的錯誤通知。在生命週期事件工具欄我註冊後看不到後臺任務。一些更多的屏幕,這些點捕捉...
登記的電話
生命週期活動工具欄
最後一兩件事我注意到的是,當我註冊它時,我在後臺任務上設置了返回null ...(m aybe我的錯誤是在這裏),下面是這個圖片:
UPDATE
更新顯示沒有後臺任務的生命週期事件的屏幕捕獲。另外我注意到,如果我添加「系統事件」的包。appxmanifest並將我的觸發器更改爲系統事件當註冊後臺任務開始工作...