azure - 来自 Xamarin 中 Azure 通知中心的后台通知
问题描述
我有一个 Azure 通知中心,它成功地在前台向 Android 和 iOS 发送通知。
以下是我的 APNS 模板:
const string templateBodyAPNS = "{ aps = { \"content-available\" = 1; }; Message = ${messageParam}; id=${id; }";
这工作正常,我可以通过代码在前台处理通知并点击断点等
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action completionHandler)
但是,public override void DidReceiveRemoteNotification(UIApplication application,NSDictionary userInfo, Action completionHandler)
当应用程序在后台时,似乎没有获得通知。我添加了从未调用过的断点和调试语句。但是,通知仍会在后台触发。
我有什么明显的遗漏吗?
[编辑]
当在后台收到应用程序的通知时,此方法不会触发。
public override void DidReceiveRemoteNotification(UIApplication application,
NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
string alert = string.Empty;
if (aps.ContainsKey(new NSString("alert")))
alert = (aps[new NSString("alert")] as NSString).ToString();
// Show alert
if (!string.IsNullOrEmpty(alert))
{
var notificationAlert = UIAlertController.Create("Notification", alert, UIAlertControllerStyle.Alert);
notificationAlert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Cancel, null));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(notificationAlert, true, null);
}
}
当应用程序处于前台并工作时,此方法会触发通知(我获取远程通知并创建本地通知。
public override void WillPresentNotification(UNUserNotificationCenter
center, UNNotification notification,
Action<UNNotificationPresentationOptions> completionHandler)
{
if (notification.Request.Content.Body != null)
{
string[] notifdata = notification.Request.Content.Body.Split(',');
if (notifdata.Length > 1)
{
var content = new UNMutableNotificationContent();
content.Title = "New News Item";
content.Subtitle = notifdata[0];
//content.Body = "Body";
content.Badge = 1;
content.CategoryIdentifier = notifdata[1];
content.Sound = UNNotificationSound.Default;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(10, false);
var requestID = "notificationRequest";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();
UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
{
if (err != null)
{
// Report error
System.Console.WriteLine("Error: {0}", err);
}
else
{
var runcount = Preferences.Get("count", 0);
// Report Success
System.Console.WriteLine("Count is" + runcount);
System.Console.WriteLine("Notification Scheduled: {0}", request);
}
});
}
谢谢!
解决方案
推荐阅读
- javascript - jQuery根据选择字段隐藏表单?
- snowflake-cloud-data-platform - 除法运算符导致大量精度
- r - 如何同时在两个数据集中使用 - R
- c# - 使用 MVC 加载视图时如何解决此错误
- python - 列表上的逻辑函数 - 奇怪的 python 行为
- xml - 使用 XSLT 将元素转换为属性时如何在 XML 中设置命名空间?
- c# - 用 JObject 过滤 c# 对象
- pandas - 如何解开熊猫数据框以获取计数
- javascript - PackBits 算法的实现
- css - 如何结合双色调效果和 CSS mix-blend-mode:difference