1

I am trying to implement Push notifications using Azure's Notifications Hub in Xamarin forms. Currently I am doing this for Android, using GCM and I follow this link. But in Step 5 of the tutorial in 'Adding push notifications to the droid project', I get the error that modifier static is not valid for this item. After this step, this is what my code looks like:

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create a new instance field for this activity.
        static MainActivity instance = null;
        // Set the current instance of MainActivity.
        instance = this;

        global::Xamarin.Forms.Forms.Init (this, bundle);
        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        LoadApplication (new App ());
        try
        {
            // Check to ensure everything's setup right
            GcmClient.CheckDevice(this);
            GcmClient.CheckManifest(this);

            // Register for push notifications
            System.Diagnostics.Debug.WriteLine("Registering...");
            GcmClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
        }
        catch (Java.Net.MalformedURLException)
        {
            CreateAndShowDialog("There was an error creating the Mobile Service. Verify the URL", "Error");
        }
        catch (Exception e)
        {
            CreateAndShowDialog(e.Message, "Error");
        }
        private void CreateAndShowDialog(String message, String title)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.SetMessage(message);
        builder.SetTitle(title);
        builder.Create().Show();
    }
    // Return the current activity instance.
    public static MainActivity CurrentActivity
    {
        get
        {
            return instance;
        }
    }
}

I am new to C# and Xamarin forms, and cannot figure out where I am going wrong.

Charu Bansal
  • 85
  • 1
  • 2
  • 13
  • please learn basic C# syntax ... it seems like compile time error(which means that you don't know C# basics) and it exactly tells you what is wrong and in which line... – Selvin Jun 29 '16 at 17:26
  • 1
    Possible duplicate of [Does C# support the use of static local variables?](http://stackoverflow.com/questions/2393156/does-c-sharp-support-the-use-of-static-local-variables) – Selvin Jun 29 '16 at 17:28

2 Answers2

2

You need to declare the static variable outside of the function.

private static MainActivity instance = null;
// Return the current activity instance.
public static MainActivity CurrentActivity
{
    get
    {
        return instance;
    }
}

And then simply instance = this; inside OnCreate.

tsandy
  • 911
  • 4
  • 10
  • Declaring the static variable outside of the OnCreate method removed the error. But then I am getting a parenthesis '}' expected at the end of the following method: catch (Exception e) { CreateAndShowDialog(e.Message, "Error"); } – Charu Bansal Jun 29 '16 at 17:29
  • It looks like before the line `private void CreateAndShowDialog(String message, String title)` you need a closing bracket `}` to close the `OnCreate` method. – tsandy Jun 29 '16 at 17:43
  • 1
    Yeah, thanks a lot. That helped. I was stuck on this for sometime now. Thanks, big time! – Charu Bansal Jun 29 '16 at 17:51
0

The problem is you are trying to create a static variable inside of a method. If you take another look at the example in that link, you will notice that variable is outside of the method.

Dan Harms
  • 4,725
  • 2
  • 18
  • 28