Search code examples
c#xamarin.androidxamarin.iosmonogamevisual-studio-app-center

Visual Studio App Center: The 'await' operator can only be used within an async method


I need help with App Center push notifications. I want to find out if the user has enabled or disabled push notifications on his iOS/Android device. This task should be done when the application launches. I followed this App Center tutorial but I get an error message when I check if push notifications are enabled or disabled.

App Center tutorial

Error CS4033: The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

What is wrong? How can I find out if the user has enabled or disabled push notifications?

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

Solution

  • A method that uses await must be marked as async. Try to add the async keyword before void FinishedLaunching:

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