【问题标题】:DidReceiveRemoteNotification is not fired in background mode in Xamarin FormsDidReceiveRemoteNotification 在 Xamarin Forms 的后台模式下未触发
【发布时间】:2018-07-09 12:28:12
【问题描述】:

我正在使用 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();                        
                }
            }
        }
    }

【问题讨论】:

    标签: xamarin.forms xamarin.ios


    【解决方案1】:

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

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

    或者你可以像这样用iOS Manifest Editor更新它

    【讨论】:

    • 我已经提供了权限,并且可以在后台模式下收到通知。但问题是方法没有被解雇。所以通知计数没有增加。
    猜你喜欢
    • 2015-11-05
    • 2014-09-19
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多