The example in this article describes the implementation of the. NET Platform for pushing iOS messages. Share to everyone for your reference.
The following steps are implemented:
1. Allow messages to be pushed to customers in iOS applications
2, need to have Apple's certificate and password (how to obtain, online search, need to pay the fee)
3, the iphone phone, installed the iOS application
4. NET project Reference Pushsharp.apple.dll,pushsharp.core.dll (these two files are searched on the Internet, has the source code)
5, start writing code, define the global object Pushbroker pusher = new Pushbroker ();
6. Registration Method:
Copy Code code as follows:
protected void startApp ()
{
Pusher. Registerappleservice (New Applepushchannelsettings (File.readallbytes (Certificatepath), CertificatePassword));
Pusher. Ondevicesubscriptionchanged + = pusher_ondevicesubscriptionchanged;
Pusher. Ondevicesubscriptionexpired + = pusher_ondevicesubscriptionexpired;
Pusher. Onnotificationsent + = pusher_onnotificationsent;
Pusher. onnotificationfailed + = pusher_onnotificationfailed;
}
static void Pusher_onnotificationfailed (object sender, inotification notification, Exception error)
{
var n = (applenotification) notification;
Error. Message ... To get information about push errors
Log.error ("Push error message", error);
}
static void Pusher_onnotificationsent (object sender, Inotification notification)
{
After the message was pushed successfully
var n = (applenotification) notification;
N.payload.alert.body get message content for push ...
Log.error ("Push content" +n.payload.alert.body);
}
static void Pusher_ondevicesubscriptionexpired (object sender, String Expiredsubscriptionid, DateTime EXPIRATIONDATEUTC, inotification notification)
{
Remove expired Expiredsubscriptionid from the database
}
static void Pusher_ondevicesubscriptionchanged (object sender, String Oldsubscriptionid, String Newsubscriptionid, Inotification notification)
{
Update the Oldsubscriptionid in the database to Newsubscriptionid
}
There are two parameters in the StartApp () method:
Certificatepath: Path to Certificate
Certificatepassword: Password
7, Push the code:
Copy Code code as follows:
Pusher. Queuenotification (New Applenotification (). Fordevicetoken (Tokenid). Withalert ("Push content"). Withbadge (1). Withsound ("Default"))//Tokenid from the database or other places to get the device, one for each iphone Tokenid
8, ready after these can be tested, I personally tested through, if there is any do not understand where to welcome the message exchange!
9, if you want to push on Android devices, the project to introduce PushSharp.Android.dll, code words later will be updated for everyone, please pay attention!
10, complete example code click here to download the site.
I hope this article will help you with your. NET programming.