Introduction
This tip simply puts forth the straight forward answer for how to send push notifications to iOS and Android devices using PushSharp. To download the DLL and for raw information, visit https://github.com/Redth/PushSharp.
Using the Code
Let me set the stage first. You have a page where the admin will write some message, on submit you require to push this message to all your ios and Android subscribers. For this purpose, I am using pushsharp
that I have successfully used many times. I take no credit in developing this library, but I am merely trying to assemble things for ready to use.
Let's start.
Step 1: Add namespaces
using PushSharp;
using PushSharp.Android;
using PushSharp.Apple;
using PushSharp.Core;
Step 2: Getting the Events Ready
static void DeviceSubscriptionChanged(object sender,
string oldSubscriptionId, string newSubscriptionId, INotification notification)
{
}
static void NotificationSent(object sender, INotification notification)
{
}
static void NotificationFailed(object sender,
INotification notification, Exception notificationFailureException)
{
}
static void ChannelException
(object sender, IPushChannel channel, Exception exception)
{
}
static void ServiceException(object sender, Exception exception)
{
}
static void DeviceSubscriptionExpired(object sender,
string expiredDeviceSubscriptionId,
DateTime timestamp, INotification notification)
{
}
static void ChannelDestroyed(object sender)
{
}
static void ChannelCreated(object sender, IPushChannel pushChannel)
{
}
Step 3: The Method
Following is my method to send the notification. txtNotification
is my textbox
wherein user types the message to be sent:
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtNotification.Text))
{
.... code continued in further steps
}
}
Step 4: Wire Up All the Events
var push = new PushBroker();
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;
Step 5: Get All Device ids to Which Message has to be Sent
List<Device> rows =
new List<Device>(CommonMethods.entity.Devices.ToList());
foreach (Device row in rows)
{
Step 6: Handling ios Notification
Ask your ios developer for the key and password. Attach the key file in your solution and the rest is below. Don't worry, it's only three lines, rest is all commenting:
if (row.devicename == "ios")
{
try
{
var appleCert = File.ReadAllBytes(Server.MapPath("Resources/key.p12"));
push.RegisterAppleService(new ApplePushChannelSettings(true, appleCert, "password"));
push.QueueNotification(new AppleNotification()
.ForDeviceToken(row.deviceid) .WithAlert(txtNotification.Text) .WithBadge(1)
.WithSound("sound.caf")
);
}
catch (Exception ex)
{
throw ex;
}
}
Step 7: Handling the Android
Ask your Android developer for the key and password. Attach the key file in your solution and the rest is below. Don't worry, it's only two lines, rest is all commenting:
if(row.devicename == "android")
{
push.RegisterGcmService(new
GcmPushChannelSettings("YOUR Google API's Console API Access API KEY for Server Apps HERE"));
push.QueueNotification(new GcmNotification().ForDeviceRegistrationId(
"DEVICE REGISTRATION ID HERE")
.WithJson("{\"alert\":\"Hello World!\",
\"badge\":7,\"sound\":\"sound.caf\"}"));
}
Step 8: Stop and Wait for the Queues to Drain
push.StopAllServices(waitForQueuesToFinish: true);
That's it, we are good to go.