|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +using Android.App; |
| 8 | +using Android.Content; |
| 9 | +using Android.OS; |
| 10 | +using Android.Runtime; |
| 11 | +using Android.Views; |
| 12 | +using Android.Widget; |
| 13 | +using Android.Util; |
| 14 | +using PushSharp.Client.MonoForAndroid; |
| 15 | + |
| 16 | +namespace PushSharp.ClientSample.MonoForAndroid |
| 17 | +{ |
| 18 | + //You must subclass this! |
| 19 | + [BroadcastReceiver(Permission = C2dmClient.GOOGLE_PERMISSION_C2DM_SEND)] |
| 20 | + [IntentFilter(new string[] { C2dmClient.GOOGLE_ACTION_C2DM_INTENT_RECEIVE }, |
| 21 | + Categories = new string[] { "com.pushsharp.test" })] |
| 22 | + [IntentFilter(new string[] { C2dmClient.GOOGLE_ACTION_C2DM_INTENT_REGISTRATION }, |
| 23 | + Categories = new string[] { "com.pushsharp.test" })] |
| 24 | + //[C2dmReceiver] |
| 25 | + //[C2dmReceiveIntentFilter("c2dmsharp.client.sample")] |
| 26 | + //[C2dmRegistrationIntentFilter("c2dmsharp.client.sample")] |
| 27 | + public class SampleBroadcastReceiver : C2dmBroadcastReceiver<PushService> |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + [Service] //Must use the service tag |
| 32 | + public class PushService : C2dmService |
| 33 | + { |
| 34 | + public override void OnRegistrationError(Exception ex) |
| 35 | + { |
| 36 | + Log.Error("C2DM-Sharp-Service", "Registration Failed: " + ex.Message); |
| 37 | + |
| 38 | + //Create notification or do something useful |
| 39 | + } |
| 40 | + |
| 41 | + public override void OnRegistered(string registrationId) |
| 42 | + { |
| 43 | + Log.Info("C2DM-Sharp-Service", "Registered: " + registrationId); |
| 44 | + //Send back to the server |
| 45 | + // var wc = new WebClient(); |
| 46 | + // var result = wc.UploadString("http://your.server.com/api/register/", "POST", |
| 47 | + // "{ 'registrationId' : '" + registrationId + "' }"); |
| 48 | + |
| 49 | + createNotification("C2DM# Registered...", "The device has been Registered, Tap to View!"); |
| 50 | + } |
| 51 | + |
| 52 | + public override void OnUnregistered(string lastRegistrationId) |
| 53 | + { |
| 54 | + Log.Info("C2DM-Sharp-Service", "Unregistered: " + lastRegistrationId); |
| 55 | + //Remove from the web service |
| 56 | + // var wc = new WebClient(); |
| 57 | + // var result = wc.UploadString("http://your.server.com/api/unregister/", "POST", |
| 58 | + // "{ 'registrationId' : '" + lastRegistrationId + "' }"); |
| 59 | + |
| 60 | + createNotification("C2DM# Unregistered...", "The device has been unregistered, Tap to View!"); |
| 61 | + } |
| 62 | + |
| 63 | + public override void OnMessageReceived(Bundle extras) |
| 64 | + { |
| 65 | + Log.Info("C2DM-Sharp-Service", "Message Received!"); |
| 66 | + |
| 67 | + var msg = new StringBuilder(); |
| 68 | + foreach (var key in extras.KeySet()) |
| 69 | + msg.AppendLine(key + "=" + extras.Get(key).ToString()); |
| 70 | + |
| 71 | + //Store the message |
| 72 | + var prefs = GetSharedPreferences("c2dm.client.sample", FileCreationMode.Private); |
| 73 | + var edit = prefs.Edit(); |
| 74 | + edit.PutString("last_msg", msg.ToString()); |
| 75 | + edit.Commit(); |
| 76 | + |
| 77 | + createNotification("C2DM# Msg Rec'd", "Message Received for C2DM-Sharp... Tap to View!"); |
| 78 | + } |
| 79 | + |
| 80 | + void createNotification(string title, string desc) |
| 81 | + { |
| 82 | + //Create notification |
| 83 | + var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; |
| 84 | + |
| 85 | + //Create an intent to show ui |
| 86 | + var uiIntent = new Intent(this, typeof(DefaultActivity)); |
| 87 | + |
| 88 | + //Create the notification |
| 89 | + var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title); |
| 90 | + |
| 91 | + //Auto cancel will remove the notification once the user touches it |
| 92 | + notification.Flags = NotificationFlags.AutoCancel; |
| 93 | + |
| 94 | + //Set the notification info |
| 95 | + //we use the pending intent, passing our ui intent over which will get called |
| 96 | + //when the notification is tapped. |
| 97 | + notification.SetLatestEventInfo(this, |
| 98 | + title, |
| 99 | + desc, |
| 100 | + PendingIntent.GetActivity(this, 0, uiIntent, 0)); |
| 101 | + |
| 102 | + //Show the notification |
| 103 | + notificationManager.Notify(1, notification); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments