首页 > 解决方案 > 来自 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);


                        }


                    });



                }

谢谢!

标签: azurexamarinazure-notificationhub

解决方案


推荐阅读