Xamarin Forms中未在后台模式下触发DidReceiveRemoteNotification

时间:2018-07-09 12:28:12

标签: xamarin.forms xamarin.ios

我正在使用Xamarin Forms,并使用Azure实施了推送通知。应用程序在前景中触发了DidReceiveRemoteNotification方法。但是在后台模式下不会调用它。我在这种方法中存储“通知接收计数”,并且没有增加。我在设备上使用iOS版本11.4。我该如何解决?我在DidReceivedRemoteNotification方法中调用下面的方法。

void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
    {            
        if (null != options && options.ContainsKey(new NSString("aps")))
        {               
            NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;

            string alert = string.Empty;
            string type = string.Empty;

            if (aps.ContainsKey(new NSString("alert")))
                alert = (aps[new NSString("alert")] as NSString).ToString();
            if (aps.ContainsKey(new NSString("type")))
                type = (aps[new NSString("type")] as NSString).ToString();

            if (type.ToLower() == "menu")
            {
                SettingClass.MenuNotification = true;
                SettingClass.MenuNotificationCount += 1;
                MenuCount += 1;
                NSUserDefaults.StandardUserDefaults.SetString(MenuCount.ToString(), "MenuNotification");
            }
            if (type.ToLower() == "promo" || type.ToLower() == "promotion")
            {
                SettingClass.PromoNotification = true;
                SettingClass.PromoNotificationCount += 1;
                PromoCount += 1;
                NSUserDefaults.StandardUserDefaults.SetString(PromoCount.ToString(), "PromoNotification");                   
            }


            if (!fromFinishedLaunching)
            {
                //Manually show an alert
                if (!string.IsNullOrEmpty(alert))
                {
                    MessagingCenter.Send("UpdateTabs", "NotificationCount");
                    UIAlertView avAlert = new UIAlertView(type, alert, null, "OK", null);
                    avAlert.Show();                        
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果要在后台处理远程通知,则需要通过添加下一行来更新.plist文件

<key>UIBackgroundModes</key>
<array>
    ...some other permission...
    <string>remote-notification</string>
</array>

或者您可以像这样用iOS Manifest Editor更新它 enter image description here