zoukankan      html  css  js  c++  java
  • asp.net IOS推送消息

    撰写的第一篇博文,希望能够帮助急用的人。

    此文意在指导大家怎么用asp.net 向apns推送消息(如有错误的地方欢迎纠正)

    步骤如下:

     1:需要正确的P12证书和密码,这里注意p12证书有两个版本,一个是开发版本,一个是正式版本。开发版本是用来开发时候测试用的,正式版本则是项目正式上线之后用的。将p12文件导入到项目中。网上有很多p12文件生成的例子给个参考的链接地址P12证书生成,这里就不多废话了。

     2:项目中需要引进JdSoft.Apns.Notifications.dll(下载地址) 不过这个地址需要FQ才能下载。

    前期准备工作做好之后下边是具体代码实现:

     1   var p12key = "";//这里是p12文件的相对地址
     2 //获取p12文件的物理地址
     3  
     4  var p12File = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, p12key);
     5            var service1 = new NotificationService(false, p12File, p12password, 1);
     6 int sleepBetweenNotifications = 5000; //停歇10秒之后再执行
     7 
     8             service1.SendRetries = 5;  //重试连接次数
     9             service1.ReconnectDelay = 5000;  //延迟多久重新连接
    10 
    11             service1.Error += new NotificationService.OnError(service_Error);
    12             service1.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);
    13             service1.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
    14             service1.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
    15             service1.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
    16             service1.Connecting += new NotificationService.OnConnecting(service_Connecting);
    17             service1.Connected += new NotificationService.OnConnected(service_Connected);
    18             service1.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
    19 
    20 //定义消息体
    21  var alertNotification = new Notification(token.Token);
    22                             alertNotification.Payload.Badge = i;
    23                             alertNotification.Payload.Sound = "default";
    24                             alertNotification.Payload.Alert.Body = message.MessageContent;
    25 
    26                             if (service1.QueueNotification(alertNotification))
    27                                 LogHelper.Record("给IOS用户:" + token.Token + "推送一条消息:" + message.MessageContent);//写入日志,方法自定义的
    28                             else
    29                                 LogHelper.Record("Notification Failed to be Queued!");
    30 
    31 //设置服务器向客户端发送消息的时间间隔
    32 System.Threading.Thread.Sleep(sleepBetweenNotifications);
    33 
    34 //关闭服务
    35 service1.Close();
    36 //释放内存
    37 service1.Dispose();
    38 
    39  static void service_BadDeviceToken(object sender, BadDeviceTokenException ex)
    40         {
    41             LogHelper.Record(String.Format("Bad Device Token: {0}", ex.Message));
    42         }
    43 
    44         static void service_Disconnected(object sender)
    45         {
    46             LogHelper.Record("Disconnected......");
    47         }
    48 
    49         static void service_Connected(object sender)
    50         {
    51             LogHelper.Record("Connected......");
    52         }
    53 
    54         static void service_Connecting(object sender)
    55         {
    56             LogHelper.Record("Connecting......");
    57         }
    58 
    59         static void service_NotificationTooLong(object sender, NotificationLengthException ex)
    60         {
    61             LogHelper.Record(String.Format("Notification Too Long: {0}", ex.Notification.ToString()));
    62         }
    63 
    64         static void service_NotificationSuccess(object sender, Notification notification)
    65         {
    66             LogHelper.Record(String.Format("Notification Success: {0}", notification.ToString()));
    67         }
    68 
    69         static void service_NotificationFailed(object sender, Notification notification)
    70         {
    71             LogHelper.Record(String.Format("Notification Failed: {0}", notification.ToString()));
    72         }
    73 
    74         static void service_Error(object sender, Exception ex)
    75         {
    76             LogHelper.Record(String.Format("Error: {0}", ex));
    77         }
    78     }
    View Code

    方法定义好之后调用:

        QueueToApple queue = new QueueToApple();

    queue.QueueStart(msgList);//msgList为消息列表

    到此,基本已经大功告成了。

     重点提一下这里var service1 = new NotificationService(false, p12File, p12password, 1);NotificationService(这个方法里面的第一个参数:true是代表当前启用的是开发版本的p12文件,所以后边的p12文件路径需要是开发版本的,false是启用正式版本的,那么后边的p12文件路径就需要是正式p12文件的路径。

  • 相关阅读:
    模板的导入和继承
    图解从 URL 到网页通信原理
    HTTP协议详细介绍
    django的模型层
    LeetCode3-Longest Substring Without Repeating Characters
    LeetCode2-Add Two Numbers
    LeetCode1-Two Sum
    面试干货整理
    VS中使用QT调用R脚本
    MFC DLL 资源模块句柄切换[转]
  • 原文地址:https://www.cnblogs.com/xiao-qian/p/4539138.html
Copyright © 2011-2022 走看看