PushSharp是一个C#编写的服务端类库,用于推送消息到各种客户端,支持iOS(iPhone/iPad)、Android、Windows Phone、Windows 8、Amazo、Blackberry等设备。
官方网站:https://github.com/Redth/PushSharp
当前最新稳定版本为2.0.4,支持通过NuGet获取(https://www.nuget.org/packages/PushSharp/)
提供了易于使用的API,支持以下平台的消息推送:
100%的托管代码完全兼容Mono平台。
PushSharp主要包含以下程序集:
其中,PushSharp.Core为必须的组件,其他的可以根据自己需要来选择对应平台。
平常使用只需要用NuGet来获取程序集即可:
class="brush: bash; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 2; toolbar: true;">Install-Package PushSharp
这样会把主流平台的程序集(Apple/Android/Windows/WindowsPhone)都下载下来,可以根据自己需要删除用不到的平台组件。
假如需要使用Blackberry等NuGet包里没有的组件,则需要到官方网站(https://github.com/Redth/PushSharp)获取源码自行编译。
对于Apple平台,只需要PushSharp.Core和PushSharp.Apple组件即可。
官方WIKI提供了详细的证书配置步骤:
Apple平台证书创建流程:
对于Apple平台需要特别说明:
启用消息推送都是在AppDelegate里注册来完成的。
对于使用objc语言编写的客户端:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (application.enabledRemoteNotificationTypes == UIRemoteNotificationTypeNone)
{
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound];
}
application.applicationIconBadgeNumber = -1;
// Override point for customization after application launch.
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *pushToken = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<"withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[[NSUserDefaults standardUserDefaults] setObject:pushToken forKey:@"pushtoken"]; // 保存起来
}
对于使用MonoTouch的客户端:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
app.RegisterForRemoteNotificationTypes(
UIRemoteNotificationType.Alert |
UIRemoteNotificationType.Badge |
UIRemoteNotificationType.Sound);
app.ApplicationIconBadgeNumber = -1;
// ...
return true;
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
string tokenStr = token.Description;
string pushToken = tokenStr.Replace("<", string.Empty).Replace(">", string.Empty).Replace(" ", string.Empty);
NSUserDefaults.StandardUserDefaults.SetString(pushToken, "pushtoken");
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
}
在接收到deviceToken的时候先存储在NSUserDefaults中,在用户登录的时候再取出来一起发送到服务端:
NSString *pushToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"pushtoken"]; //objc
string pushToken = NSUserDefaults.StandardUserDefaults.StringForKey("pushtoken"); //MonoTouch
服务端在用户登录成功之后,把接收到用户的用户名与pushToken关联起来,在推送消息的时候就可以针对指定用户来推送,具体的过程略。
而对于不需要用户登录的app,可以在接收到deviceToken的时候直接发送到服务端。
更多的客户端配置参考PushSharp源码的Client.Samples及PushSharp.Client目录。
var pusher = new PushBroker();
pusher.RegisterAppleService(new ApplePushChannelSettings(File.ReadAllBytes("yourAppId.p12"), "证书的密码"));
pusher.QueueNotification(
new AppleNotification()
.ForDeviceToken(pushToken) // 从数据库等地方获取设备的pushToken
.WithAlert("测试iOS消息推送 - 囧月")
.WithBadge(1)
.WithSound("default")
);
在RegisterAppleService方法中可以注册多个APNS证书,PushSharp可以自动检测是Development/Production,这时候需要为证书设置标识:
pusher.RegisterAppleService(new ApplePushChannelSettings(File.ReadAllBytes("yourAppId.p12"), "证书的密码"), "证书标识如youAppId_development");
pusher.RegisterAppleService(new ApplePushChannelSettings(File.ReadAllBytes("yourAppId.p12"), "证书的密码"), "证书标识如youAppId_production");
此外,可以注册各种事件来获得各种状态:
pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged;
pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired;
pusher.OnNotificationSent += pusher_OnNotificationSent;
pusher.OnNotificationFailed += pusher_OnNotificationFailed;
pusher.OnNotificationRequeue += pusher_OnNotificationRequeue;
pusher.OnChannelCreated += pusher_OnChannelCreated;
pusher.OnChannelDestroyed += pusher_OnChannelDestroyed;
pusher.OnChannelException += pusher_OnChannelException;
pusher.OnServiceException += pusher_OnServiceException;
static void pusher_OnNotificationFailed(object sender, INotification notification, Exception error)
{
var n = (AppleNotification)notification;
//error.Message ...获取推送出错的信息
}
static void pusher_OnNotificationSent(object sender, INotification notification)
{
//消息推送成功后
var n = (AppleNotification)notification;
//n.Payload.Alert.Body 获取推送的消息内容...
}
static void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, INotification notification)
{
// 从数据库删除过期的expiredSubscriptionId
}
static void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
{
// 把数据库中的oldSubscriptionId更新为newSubscriptionId
}
更多请参考源码的PushSharp.Sample目录。
官方网站:https://github.com/Redth/PushSharp 可以获取最新源码及各种例子
WIKI:https://github.com/Redth/PushSharp/wiki 详细说明了各平台证书配置的方法
Xamarin相关文档:http://docs.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/