Visual Studio App Center:'await'运算符只能在异步方法中使用

问题描述 投票:0回答:2

我需要有关App Center推送通知的帮助。我想知道用户是否在他的iOS / Android设备上启用或禁用了推送通知。应用程序启动时应执行此任务。我遵循了这个App Center教程但是当我检查推送通知是启用还是禁用时,我收到一条错误消息。

App Center tutorial

错误CS4033:'await'运算符只能在异步方法中使用。考虑使用'async'修饰符标记此方法并将其返回类型更改为'Task'。

怎么了?如何确定用户是否已启用或禁用推送通知?

using Foundation;
using UIKit;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.AppCenter.Push;

namespace iosprojectnew.iOS
{
    [Register("AppDelegate")]
    class Program : UIApplicationDelegate
    {
        private static Game1 game;

        internal static void RunGame()
        {
            game = new Game1();
            game.Run();
        }

        static void Main(string[] args)
        {
            UIApplication.Main(args, null, "AppDelegate");
        }

        public override void FinishedLaunching(UIApplication app)
        {      
            if (!AppCenter.Configured)
            {
                bool isEnabled = await Push.IsEnabledAsync();

                Push.PushNotificationReceived += (sender, e) =>
                {
                    // Add the notification message and title to the message
                    var summary = $"Push notification received:" +
                                        $"\n\tNotification title: {e.Title}" +
                                        $"\n\tMessage: {e.Message}";

                    // If there is custom data associated with the notification,
                    // print the entries
                    if (e.CustomData != null)
                    {
                        summary += "\n\tCustom data:\n";
                        foreach (var key in e.CustomData.Keys)
                        {
                            summary += $"\t\t{key} : {e.CustomData[key]}\n";
                        }
                    }

                    // Send the notification summary to debug output
                    System.Diagnostics.Debug.WriteLine(summary);
                };
            }


            AppCenter.Start("...", typeof(Analytics), typeof(Crashes), typeof(Push));
            RunGame();
        }
    }
}
c# xamarin.android xamarin.ios monogame visual-studio-app-center
2个回答
0
投票

使用await的方法必须标记为async。尝试在async之前添加void FinishedLaunching关键字:

public override async void FinishedLaunching(UIApplication app) { ... }

0
投票

await关键字只能用于异步方法,但此方法是覆盖,因此您无法“转换为任务”。

无论如何,您可以像这样编写使用异步的行代码:

bool isEnabled = await Push.IsEnabledAsync().Result;
© www.soinside.com 2019 - 2024. All rights reserved.