PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Client.Samples/PushSharp.ClientSample.MonoForAndroid/PushSharp.ClientSample.MonoForAndroid.Gcm/PushService.cs

https://github.com/mustafagenc/PushSharp
C# | 128 lines | 78 code | 24 blank | 26 comment | 4 complexity | 41257ebbf9253f2d86abe4754ef6f731 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Android.App;
  6. using Android.Content;
  7. using Android.OS;
  8. using Android.Runtime;
  9. using Android.Views;
  10. using Android.Widget;
  11. using Android.Util;
  12. using GCMSharp.Client;
  13. namespace PushSharp.ClientSample.MonoForAndroid
  14. {
  15. //You must subclass this!
  16. [BroadcastReceiver(Permission=GCMConstants.PERMISSION_GCM_INTENTS)]
  17. [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_MESSAGE },
  18. Categories = new string[] { "com.pushsharp.test" })]
  19. [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_REGISTRATION_CALLBACK },
  20. Categories = new string[] { "com.pushsharp.test" })]
  21. [IntentFilter(new string[] { GCMConstants.INTENT_FROM_GCM_LIBRARY_RETRY },
  22. Categories = new string[] { "com.pushsharp.test" })]
  23. //[C2dmReceiver]
  24. //[C2dmReceiveIntentFilter("c2dmsharp.client.sample")]
  25. //[C2dmRegistrationIntentFilter("c2dmsharp.client.sample")]
  26. public class SampleBroadcastReceiver : GCMBroadcastReceiver<GCMIntentService>
  27. {
  28. //IMPORTANT: Change this to your own Sender ID!
  29. //The SENDER_ID is your Google API Console App Project ID.
  30. // 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,
  31. // but instead the numeric project id in the url: eg: https://code.google.com/apis/console/?pli=1#project:785671162406:overview
  32. // where 785671162406 is the project id, which is the SENDER_ID to use!
  33. public const string SENDER_ID = "785671162406";
  34. public const string TAG = "PushSharp-GCM";
  35. }
  36. [Service] //Must use the service tag
  37. public class GCMIntentService : GCMBaseIntentService
  38. {
  39. public GCMIntentService() : base(SampleBroadcastReceiver.SENDER_ID) {}
  40. protected override void OnRegistered (Context context, string registrationId)
  41. {
  42. Log.Verbose(SampleBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
  43. //Send back to the server
  44. // var wc = new WebClient();
  45. // var result = wc.UploadString("http://your.server.com/api/register/", "POST",
  46. // "{ 'registrationId' : '" + registrationId + "' }");
  47. createNotification("PushSharp-GCM Registered...", "The device has been Registered, Tap to View!");
  48. }
  49. protected override void OnUnRegistered (Context context, string registrationId)
  50. {
  51. Log.Verbose(SampleBroadcastReceiver.TAG, "GCM Unregistered: " + registrationId);
  52. //Remove from the web service
  53. // var wc = new WebClient();
  54. // var result = wc.UploadString("http://your.server.com/api/unregister/", "POST",
  55. // "{ 'registrationId' : '" + lastRegistrationId + "' }");
  56. createNotification("PushSharp-GCM Unregistered...", "The device has been unregistered, Tap to View!");
  57. }
  58. protected override void OnMessage (Context context, Intent intent)
  59. {
  60. Log.Info(SampleBroadcastReceiver.TAG, "GCM Message Received!");
  61. var msg = new StringBuilder();
  62. if (intent != null && intent.Extras != null)
  63. {
  64. foreach (var key in intent.Extras.KeySet())
  65. msg.AppendLine(key + "=" + intent.Extras.Get(key).ToString());
  66. }
  67. //Store the message
  68. var prefs = GetSharedPreferences(context.PackageName, FileCreationMode.Private);
  69. var edit = prefs.Edit();
  70. edit.PutString("last_msg", msg.ToString());
  71. edit.Commit();
  72. createNotification("PushSharp-GCM Msg Rec'd", "Message Received for C2DM-Sharp... Tap to View!");
  73. }
  74. protected override bool OnRecoverableError (Context context, string errorId)
  75. {
  76. Log.Warn(SampleBroadcastReceiver.TAG, "Recoverable Error: " + errorId);
  77. return base.OnRecoverableError (context, errorId);
  78. }
  79. protected override void OnError (Context context, string errorId)
  80. {
  81. Log.Error(SampleBroadcastReceiver.TAG, "GCM Error: " + errorId);
  82. }
  83. void createNotification(string title, string desc)
  84. {
  85. //Create notification
  86. var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
  87. //Create an intent to show ui
  88. var uiIntent = new Intent(this, typeof(DefaultActivity));
  89. //Create the notification
  90. var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title);
  91. //Auto cancel will remove the notification once the user touches it
  92. notification.Flags = NotificationFlags.AutoCancel;
  93. //Set the notification info
  94. //we use the pending intent, passing our ui intent over which will get called
  95. //when the notification is tapped.
  96. notification.SetLatestEventInfo(this,
  97. title,
  98. desc,
  99. PendingIntent.GetActivity(this, 0, uiIntent, 0));
  100. //Show the notification
  101. notificationManager.Notify(1, notification);
  102. }
  103. }
  104. }