Skip to content

Commit

Permalink
It's ALIVE!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Jan 22, 2011
1 parent 07584cd commit 0656578
Show file tree
Hide file tree
Showing 16 changed files with 453 additions and 256 deletions.
10 changes: 6 additions & 4 deletions C2dmSharp.Client.Sample/C2dmSharp.Client.Sample.csproj
Expand Up @@ -17,6 +17,7 @@
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidUseSharedRuntime>True</AndroidUseSharedRuntime>
<AndroidSupportedAbis>armeabi</AndroidSupportedAbis>
<MonoDroidLinkMode>None</MonoDroidLinkMode>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -47,16 +48,14 @@
<Compile Include="DefaultActivity.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SampleService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\main.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\values\strings.xml" />
<AndroidResource Include="Resources\layout\main.axml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-hdpi\icon.png" />
Expand All @@ -72,6 +71,9 @@
<ItemGroup>
<Content Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\values\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
125 changes: 62 additions & 63 deletions C2dmSharp.Client.Sample/DefaultActivity.cs
Expand Up @@ -7,99 +7,98 @@
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Util;

namespace C2dmSharp.Client.Sample
{
[Activity(Label = "C2DM-Sharp Sample", MainLauncher = true)]
[Activity(Label = "C2DM-Sharp Sample", MainLauncher = true,
LaunchMode= Android.Content.PM.LaunchMode.SingleTask)]
public class DefaultActivity : Activity
{
Button buttonRegister;
Button buttonUnregister;
//NOTE: You need to put your own email here!
// Whichever one you registered as the 'Role' email with google
public const string senderIdEmail = "redthc2dm@gmail.com";


TextView textRegistrationStatus = null;
TextView textRegistrationId = null;
TextView textLastMsg = null;
Button buttonRegister = null;
bool registered = false;

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

Android.Util.Log.D("C2DM-Sample", "Hello World!");

// Set our view from the "main" layout resource
SetContentView(Resource.layout.main);
SetContentView(Resource.Layout.main);

// Get our buttons from the layout resource,
// and attach an event to it
buttonRegister = FindViewById<Button>(Resource.id.buttonRegister);
buttonUnregister = FindViewById<Button>(Resource.id.buttonUnregister);
textRegistrationStatus = FindViewById<TextView>(Resource.Id.textRegistrationStatus);
textRegistrationId = FindViewById<TextView>(Resource.Id.textRegistrationId);
textLastMsg = FindViewById<TextView>(Resource.Id.textLastMessage);
buttonRegister = FindViewById<Button>(Resource.Id.buttonRegister);

Android.Util.Log.D("C2DM-Sample", "Got Buttons...");
Log.Info("C2DM-Sharp-UI", "Hello World");

buttonUnregister.Enabled = false;

buttonRegister.Click += delegate
this.buttonRegister.Click += delegate
{
try
{
C2dmSharp.Client.C2dmClient.Register(this);
if (!registered)
{
Log.Info("C2DM-Sharp", "Registering...");
C2dmSharp.Client.C2dmClient.Register(this, senderIdEmail);
}
catch (NoGoogleAccountsOnDeviceRegistrationException ngex)
else
{
MakeToast("No Google Accounts on this Device! Please add one and try again!");
Log.Info("C2DM-Sharp", "Unregistering...");
C2dmSharp.Client.C2dmClient.Unregister(this);
}

buttonRegister.Enabled = false;
};

buttonUnregister.Click += delegate
{
C2dmSharp.Client.C2dmClient.Unregister(this);
buttonUnregister.Enabled = false;
};


Android.Util.Log.D("C2DM-Sample", "Registered Button Clicks...");

C2dmSharp.Client.C2dmClient.ReceiveMessage += new Action<Bundle>(C2dmClient_ReceiveMessage);
C2dmSharp.Client.C2dmClient.Registered += new Action<string>(C2dmClient_Registered);
C2dmSharp.Client.C2dmClient.RegisterError += new Action<Exception>(C2dmClient_RegisterError);
C2dmSharp.Client.C2dmClient.Unregistered += new Action(C2dmClient_Unregistered);

Android.Util.Log.D("C2DM-Sample", "Registered Client Events...");
RunOnUiThread(() =>
{
//Disable the button so that we can't click it again
//until we get back to the activity from a notification
this.buttonRegister.Enabled = false;
});
};
}

void C2dmClient_Unregistered()
protected override void OnResume()
{
MakeToast("C2DM Unregistered");

if (buttonRegister != null)
buttonRegister.Enabled = true;
}
base.OnResume();

void C2dmClient_RegisterError(Exception ex)
{
MakeToast(ex.Message);
updateView();
}

void C2dmClient_Registered(string registrationId)
void updateView()
{
MakeToast("C2DM Registered (ID: " + registrationId + ")");

if (buttonUnregister != null)
buttonUnregister.Enabled = true;
}
//Get the stored latest registration id
var registrationId = C2dmClient.GetRegistrationId(this);

void C2dmClient_ReceiveMessage(Bundle extras)
{
var msg = new StringBuilder();
msg.AppendLine("C2DM Received Message");
//If it's empty, we need to register
if (string.IsNullOrEmpty(registrationId))
{
registered = false;
this.textRegistrationStatus.Text = "Registered: No";
this.textRegistrationId.Text = "Id: N/A";
this.buttonRegister.Text = "Register...";

Log.Info("C2DM-Sharp", "Not registered...");
}
else
{
registered = true;
this.textRegistrationStatus.Text = "Registered: Yes";
this.textRegistrationId.Text = "Id: " + registrationId;
this.buttonRegister.Text = "Unregister...";

foreach (var key in extras.KeySet())
msg.AppendLine(" " + key + "=" + extras.Get(key).ToString());
Log.Info("C2DM-Sharp", "Already Registered: " + registrationId);
}

MakeToast(msg.ToString());
}
var prefs = GetSharedPreferences("c2dm.client.sample", FileCreationMode.Private);
this.textLastMsg.Text = "Last Msg: " + prefs.GetString("last_msg", "N/A");

void MakeToast(string msg)
{
Toast.MakeText(this, msg, ToastLength.Short).Show();
//Enable the button as it was normally disabled
this.buttonRegister.Enabled = true;
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions C2dmSharp.Client.Sample/Properties/AndroidManifest.xml
Expand Up @@ -3,15 +3,13 @@
<application android:label="C2dmSharp.Client.Sample">
</application>
<uses-sdk android:minSdkVersion="8" />


<permission android:name="c2dmsharp.client.sample.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<permission android:name="c2dmsharp.client.sample.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="c2dmsharp.client.sample.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive message -->

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- This app has permission to register and receive message -->
<!-- Send the registration id to the server -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
49 changes: 19 additions & 30 deletions C2dmSharp.Client.Sample/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions C2dmSharp.Client.Sample/Resources/layout/main.axml
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textRegistrationStatus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Registered: No"
android:paddingBottom="5dp" />
<TextView android:id="@+id/textRegistrationId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Id: N/A"
android:paddingBottom="5dp" />
<TextView android:id="@+id/textLastMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Last Msg: N/A" />
<Button android:id="@+id/buttonRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Register..." />
<TextView android:id="@+id/textHints"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NOTE: The values above are only updated AFTER you tap a notification!" />
</LinearLayout>
19 changes: 0 additions & 19 deletions C2dmSharp.Client.Sample/Resources/layout/main.xml

This file was deleted.

7 changes: 0 additions & 7 deletions C2dmSharp.Client.Sample/Resources/values/strings.xml

This file was deleted.

0 comments on commit 0656578

Please sign in to comment.