Xamarin中来自Azure通知中心的后台通知

时间:2019-04-10 14:26:57

标签: azure xamarin azure-notificationhub

我有一个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)在后​​台运行时似乎未获得通知。我添加了从未调用的断点和Debug语句。但是,该通知仍在后台触发。

有什么明显的我想念的东西吗?

[编辑]

当应用程序在后台收到通知时,此方法不会触发。

    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 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);


                        }


                    });



                }

谢谢!

0 个答案:

没有答案