当前位置: 移动技术网 > IT编程>开发语言>.net > .net平台推送ios消息的实现方法

.net平台推送ios消息的实现方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

人皮灯笼3,张瑞敏演讲,少年阴阳判官

本文实例讲述了.net平台推送ios消息的实现方法。分享给大家供大家参考。

具体实现步骤如下:

1、ios应用程序中允许向客户推送消息

2、需要有苹果的证书以及密码(怎么获取,网上搜一下,需要交费的)

3、iphone手机一部,安装了该ios应用程序

4、.net 项目中引用pushsharp.apple.dll,pushsharp.core.dll(这两个文件在网上搜一下,有源码的)

5、开始写代码,定义全局的对象pushbroker pusher = new pushbroker();

6、注册方法:

复制代码 代码如下:
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 ...获取推送出错的信息
    log.error("推送出错的信息", error);
}

static void pusher_onnotificationsent(object sender, inotification notification)
{
    //消息推送成功后
    var n = (applenotification)notification;
    //n.payload.alert.body  获取推送的消息内容...
    log.error("推送内容"+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
}

startapp()方法中有两个参数:

certificatepath:证书的路径
certificatepassword:密码

7、推送代码:

复制代码 代码如下:
pusher.queuenotification(new applenotification().fordevicetoken(tokenid)  .withalert("推送的内容").withbadge(1).withsound("default"));// 从数据库或者其他等地方获取设备的tokenid,每个iphone一个tokenid

8、准备好这些以后就可以测试,本人亲自测试通过,如果有什么不明白的地方欢迎留言交流!

9、如果想在android设备上推送,项目要引进pushsharp.android.dll,代码的话后期会为大家更新,敬请关注!

10、完整实例代码点击此处。

希望本文所述对大家的.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网