Permalink
Browse files

Made GCM Client more friendly

Refactored/Renamed some things to make the GCM Client (PushClient for
Mono for Android) easier to implement and understand, using some new
features in Mono for Android (such as the [Permission] and
[UsesPermission] attributes)...
  • Loading branch information...
1 parent 5b79c1e commit f168c843c48b2daf53bd5314c6a7690f541183f0 @Redth committed Jan 24, 2013
Showing with 186 additions and 202 deletions.
  1. +4 −3 ...shSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.C2dm/DefaultActivity.cs
  2. +1 −6 ...ntSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.C2dm/Properties/AndroidManifest.xml
  3. +20 −20 ...s/PushSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.C2dm/PushService.cs
  4. +10 −1 ...ntSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.C2dm/Resources/Resource.designer.cs
  5. +7 −7 ...ushSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/DefaultActivity.cs
  6. +5 −10 ...entSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/Properties/AndroidManifest.xml
  7. +1 −2 ....ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/Properties/AssemblyInfo.cs
  8. +27 −32 ...es/PushSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/PushService.cs
  9. +6 −4 ...ndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/PushSharp.ClientSample.MonoForAndroid.Gcm.csproj
  10. +10 −1 ...entSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/Resources/Resource.designer.cs
  11. +19 −14 Client.Samples/PushSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.sln
  12. +0 −61 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/C2dmBroadcastReceiver.cs
  13. +1 −1 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/C2dmExceptions.cs
  14. +3 −4 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/{C2dmClient.cs → PushClient.cs}
  15. +33 −0 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/PushHandlerBroadcastReceiverBase.cs
  16. +5 −5 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/{C2dmService.cs → PushHandlerServiceBase.cs}
  17. +4 −4 PushSharp.Client/PushSharp.Client.MonoForAndroid.C2dm/PushSharp.Client.MonoForAndroid.C2dm.csproj
  18. +1 −1 PushSharp.Client/PushSharp.Client.MonoForAndroid.Gcm/GCMConstants.cs
  19. +2 −2 PushSharp.Client/PushSharp.Client.MonoForAndroid.Gcm/{GCMRegistrar.cs → PushClient.cs}
  20. +4 −4 ...shSharp.Client.MonoForAndroid.Gcm/{GCMBroadcastReceiver.cs → PushHandlerBroadcastReceiverBase.cs}
  21. +18 −15 ....Client/PushSharp.Client.MonoForAndroid.Gcm/{GCMBaseIntentService.cs → PushHandlerServiceBase.cs}
  22. +5 −5 PushSharp.Client/PushSharp.Client.MonoForAndroid.Gcm/PushSharp.Client.MonoForAndroid.Gcm.csproj
@@ -7,6 +7,7 @@
using Android.Widget;
using Android.OS;
using Android.Util;
+using PushSharp.Client;
namespace PushSharp.ClientSample.MonoForAndroid
{
@@ -44,12 +45,12 @@ protected override void OnCreate(Bundle bundle)
if (!registered)
{
Log.Info("C2DM-Sharp", "Registering...");
- PushSharp.Client.MonoForAndroid.C2dmClient.Register(this, senderIdEmail);
+ PushClient.Register(this, senderIdEmail);
}
else
{
Log.Info("C2DM-Sharp", "Unregistering...");
- PushSharp.Client.MonoForAndroid.C2dmClient.Unregister(this);
+ PushClient.UnRegister(this);
}
RunOnUiThread(() =>
@@ -71,7 +72,7 @@ protected override void OnResume()
void updateView()
{
//Get the stored latest registration id
- var registrationId = PushSharp.Client.MonoForAndroid.C2dmClient.GetRegistrationId(this);
+ var registrationId = PushClient.GetRegistrationId(this);
//If it's empty, we need to register
if (string.IsNullOrEmpty(registrationId))
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.pushsharp.test">
<uses-sdk android:minSdkVersion="8" />
- <application android:label="PushSharp.ClientSample.MonoForAndroid"></application>
- <permission android:name="com.pushsharp.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="com.pushsharp.test.permission.C2D_MESSAGE" />
- <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
+ <application android:label="PushSharp C2DM"></application>
</manifest>
@@ -1,35 +1,38 @@
using System;
-using System.Collections.Generic;
-using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
-using Android.Runtime;
-using Android.Views;
-using Android.Widget;
using Android.Util;
-using PushSharp.Client.MonoForAndroid;
+using PushSharp.Client;
+
+//VERY VERY VERY IMPORTANT NOTE!!!!
+// Your package name MUST NOT start with an uppercase letter.
+// Android does not allow permissions to start with an upper case letter
+// If it does you will get a very cryptic error in logcat and it will not be obvious why you are crying!
+// So please, for the love of all that is kind on this earth, use a LOWERCASE first letter in your Package Name!!!!
+[assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")] //, ProtectionLevel = Android.Content.PM.Protection.Signature)]
+[assembly: UsesPermission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
+[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]
+
+[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]
+[assembly: UsesPermission(Name = "android.permission.INTERNET")]
+[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
namespace PushSharp.ClientSample.MonoForAndroid
{
//You must subclass this!
- [BroadcastReceiver(Permission = C2dmClient.GOOGLE_PERMISSION_C2DM_SEND)]
- [IntentFilter(new string[] { C2dmClient.GOOGLE_ACTION_C2DM_INTENT_RECEIVE },
- Categories = new string[] { "com.pushsharp.test" })]
- [IntentFilter(new string[] { C2dmClient.GOOGLE_ACTION_C2DM_INTENT_REGISTRATION },
- Categories = new string[] { "com.pushsharp.test" })]
- //[C2dmReceiver]
- //[C2dmReceiveIntentFilter("c2dmsharp.client.sample")]
- //[C2dmRegistrationIntentFilter("c2dmsharp.client.sample")]
- public class SampleBroadcastReceiver : C2dmBroadcastReceiver<PushService>
+ [BroadcastReceiver(Permission = PushClient.GOOGLE_PERMISSION_C2DM_SEND)]
+ [IntentFilter(new string[] { PushClient.GOOGLE_ACTION_C2DM_INTENT_RECEIVE }, Categories = new string[] { "@PACKAGE_NAME@" })]
+ [IntentFilter(new string[] { PushClient.GOOGLE_ACTION_C2DM_INTENT_REGISTRATION }, Categories = new string[] { "com.pushsharp.test" })]
+ public class PushHandlerBroadcastReceiver : PushHandlerBroadcastReceiverBase<PushService>
{
}
[Service] //Must use the service tag
- public class PushService : C2dmService
+ public class PushService : PushHandlerServiceBase
{
public override void OnRegistrationError(Exception ex)
{
@@ -94,10 +97,7 @@ void createNotification(string title, string desc)
//Set the notification info
//we use the pending intent, passing our ui intent over which will get called
//when the notification is tapped.
- notification.SetLatestEventInfo(this,
- title,
- desc,
- PendingIntent.GetActivity(this, 0, uiIntent, 0));
+ notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));
//Show the notification
notificationManager.Notify(1, notification);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -7,16 +7,16 @@
using Android.Widget;
using Android.OS;
using Android.Util;
+using PushSharp.Client;
namespace PushSharp.ClientSample.MonoForAndroid
{
- [Activity(Label = "C2DM-Sharp Sample", MainLauncher = true,
+ [Activity(Label = "PushSharp GCM Sample", MainLauncher = true,
LaunchMode= Android.Content.PM.LaunchMode.SingleTask)]
public class DefaultActivity : Activity
{
//NOTE: You need to put your own email here!
// Whichever one you registered as the 'Role' email with google
-
const string TAG = "PushSharp-GCM";
TextView textRegistrationStatus = null;
@@ -40,8 +40,8 @@ protected override void OnCreate(Bundle bundle)
Log.Info(TAG, "Hello World");
//Check to ensure everything's setup right
- GCMSharp.Client.GCMRegistrar.CheckDevice(this);
- GCMSharp.Client.GCMRegistrar.CheckManifest(this);
+ PushClient.CheckDevice(this);
+ PushClient.CheckManifest(this);
this.buttonRegister.Click += delegate
@@ -51,14 +51,14 @@ protected override void OnCreate(Bundle bundle)
Log.Info(TAG, "Registering...");
//Call to register
- GCMSharp.Client.GCMRegistrar.Register(this, SampleBroadcastReceiver.SENDER_ID);
+ PushClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
}
else
{
Log.Info(TAG, "Unregistering...");
//Call to unregister
- GCMSharp.Client.GCMRegistrar.UnRegister(this);
+ PushClient.UnRegister(this);
}
RunOnUiThread(() =>
@@ -80,7 +80,7 @@ protected override void OnResume()
void updateView()
{
//Get the stored latest registration id
- var registrationId = GCMSharp.Client.GCMRegistrar.GetRegistrationId(this);
+ var registrationId = PushClient.GetRegistrationId(this);
//If it's empty, we need to register
if (string.IsNullOrEmpty(registrationId))
@@ -1,11 +1,6 @@
-<?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.pushsharp.test">
- <uses-sdk android:minSdkVersion="10" />
- <application android:label="PushSharp"></application>
- <permission android:name="com.pushsharp.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="com.pushsharp.test.permission.C2D_MESSAGE" />
- <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
- <uses-permission android:name="android.permission.WAKE_LOCK" />
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pushsharp.test" android:installLocation="internalOnly" android:versionCode="2" android:versionName="2.0">
+ <uses-sdk android:targetSdkVersion="8" />
+ <application android:label="PushSharp Test GCM">
+ </application>
</manifest>
@@ -24,5 +24,4 @@
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
-//[assembly: AssemblyKeyFile("")]
-
+//[assembly: AssemblyKeyFile("")]
@@ -1,51 +1,49 @@
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
using System.Text;
-
using Android.App;
using Android.Content;
-using Android.OS;
-using Android.Runtime;
-using Android.Views;
-using Android.Widget;
using Android.Util;
-using GCMSharp.Client;
+using PushSharp.Client;
+
+//VERY VERY VERY IMPORTANT NOTE!!!!
+// Your package name MUST NOT start with an uppercase letter.
+// Android does not allow permissions to start with an upper case letter
+// If it does you will get a very cryptic error in logcat and it will not be obvious why you are crying!
+// So please, for the love of all that is kind on this earth, use a LOWERCASE first letter in your Package Name!!!!
+[assembly: Permission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")] //, ProtectionLevel = Android.Content.PM.Protection.Signature)]
+[assembly: UsesPermission(Name = "@PACKAGE_NAME@.permission.C2D_MESSAGE")]
+[assembly: UsesPermission(Name = "com.google.android.c2dm.permission.RECEIVE")]
+
+[assembly: UsesPermission(Name = "android.permission.GET_ACCOUNTS")]
+[assembly: UsesPermission(Name = "android.permission.INTERNET")]
+[assembly: UsesPermission(Name = "android.permission.WAKE_LOCK")]
namespace PushSharp.ClientSample.MonoForAndroid
{
//You must subclass this!
[BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)]
- [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE },
- Categories = new string[] { "com.pushsharp.test" })]
- [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
- Categories = new string[] { "com.pushsharp.test" })]
- [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY },
- Categories = new string[] { "com.pushsharp.test" })]
- //[C2dmReceiver]
- //[C2dmReceiveIntentFilter("c2dmsharp.client.sample")]
- //[C2dmRegistrationIntentFilter("c2dmsharp.client.sample")]
- public class SampleBroadcastReceiver : GCMBroadcastReceiver<GCMIntentService>
+ [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE }, Categories = new string[] { "@PACKAGE_NAME@" })]
+ [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK }, Categories = new string[] { "@PACKAGE_NAME@" })]
+ [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY }, Categories = new string[] { "@PACKAGE_NAME@" })]
+ public class PushHandlerBroadcastReceiver : PushHandlerBroadcastReceiverBase<PushHandlerService>
{
//IMPORTANT: Change this to your own Sender ID!
//The SENDER_ID is your Google API Console App Project ID.
// Be sure to get the right Project ID from your Google APIs Console. It's not the named project ID that appears in the Overview,
// but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
// where 785671162406 is the project id, which is the SENDER_ID to use!
- public const string SENDER_ID = "785671162406";
+ public static string[] SENDER_IDS = new string[] {"785671162406"};
public const string TAG = "PushSharp-GCM";
}
[Service] //Must use the service tag
- public class GCMIntentService : GCMBaseIntentService
+ public class PushHandlerService : PushHandlerServiceBase
{
- public GCMIntentService() : base(SampleBroadcastReceiver.SENDER_ID) {}
+ public PushHandlerService() : base(PushHandlerBroadcastReceiver.SENDER_IDS) { }
protected override void OnRegistered (Context context, string registrationId)
{
- Log.Verbose(SampleBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
+ Log.Verbose(PushHandlerBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
//Send back to the server
// var wc = new WebClient();
// var result = wc.UploadString("http://your.server.com/api/register/", "POST",
@@ -56,7 +54,7 @@ protected override void OnRegistered (Context context, string registrationId)
protected override void OnUnRegistered (Context context, string registrationId)
{
- Log.Verbose(SampleBroadcastReceiver.TAG, "GCM Unregistered: " + registrationId);
+ Log.Verbose(PushHandlerBroadcastReceiver.TAG, "GCM Unregistered: " + registrationId);
//Remove from the web service
// var wc = new WebClient();
// var result = wc.UploadString("http://your.server.com/api/unregister/", "POST",
@@ -67,7 +65,7 @@ protected override void OnUnRegistered (Context context, string registrationId)
protected override void OnMessage (Context context, Intent intent)
{
- Log.Info(SampleBroadcastReceiver.TAG, "GCM Message Received!");
+ Log.Info(PushHandlerBroadcastReceiver.TAG, "GCM Message Received!");
var msg = new StringBuilder();
@@ -88,14 +86,14 @@ protected override void OnMessage (Context context, Intent intent)
protected override bool OnRecoverableError (Context context, string errorId)
{
- Log.Warn(SampleBroadcastReceiver.TAG, "Recoverable Error: " + errorId);
+ Log.Warn(PushHandlerBroadcastReceiver.TAG, "Recoverable Error: " + errorId);
return base.OnRecoverableError (context, errorId);
}
protected override void OnError (Context context, string errorId)
{
- Log.Error(SampleBroadcastReceiver.TAG, "GCM Error: " + errorId);
+ Log.Error(PushHandlerBroadcastReceiver.TAG, "GCM Error: " + errorId);
}
void createNotification(string title, string desc)
@@ -115,10 +113,7 @@ void createNotification(string title, string desc)
//Set the notification info
//we use the pending intent, passing our ui intent over which will get called
//when the notification is tapped.
- notification.SetLatestEventInfo(this,
- title,
- desc,
- PendingIntent.GetActivity(this, 0, uiIntent, 0));
+ notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, 0));
//Show the notification
notificationManager.Notify(1, notification);
@@ -1,11 +1,11 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
- <ProjectGuid>{b3f5a628-e5fc-4e46-ad50-e5babc79dd3a}</ProjectGuid>
+ <ProjectGuid>{B3F5A628-E5FC-4E46-AD50-E5BABC79DD3A}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>PushSharp.ClientSample.MonoForAndroid</RootNamespace>
@@ -53,18 +53,20 @@
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
<None Include="Assets\AboutAssets.txt" />
- <None Include="Properties\AndroidManifest.xml" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\layout\Main.axml" />
<AndroidResource Include="Resources\values\Strings.xml" />
<AndroidResource Include="Resources\drawable\Icon.png" />
</ItemGroup>
- <Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\..\..\PushSharp.Client\PushSharp.Client.MonoForAndroid.Gcm\PushSharp.Client.MonoForAndroid.Gcm.csproj">
<Project>{1466d096-69fa-4b75-bbb6-533ffaf5d4eb}</Project>
<Name>PushSharp.Client.MonoForAndroid.Gcm</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup>
+ <Content Include="Properties\AndroidManifest.xml" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
Oops, something went wrong.

0 comments on commit f168c84

Please sign in to comment.