当前位置: 首页 > news >正文

Apple应用消息通知 swift前端c#后端

1. 使用keychain access->Certificate Assistent申请一个证书



2. 登录apple developer网站,申请一个push notification证书

一直到最后一步,会看到证书:





3. 下载并安装证书


4. 配置development provision file的app id






5. 配置xcode的build,选择配置好的development provision文件



6. 导出安装的证书(第3步下载的),在server端使用。





swift  代码:

class AppDelegate {


...


 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
    
        
        application.registerForRemoteNotifications()
        
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
    }




...







C# 代码:

...
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
                "push-cert.p12", "Hello-12345");


            // Create a new broker
            var apnsBroker = new ApnsServiceBroker(config);


            // Wire up events
            apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
            {
                aggregateEx.Handle(ex =>
                {
                    // See what kind of exception it was to further diagnose
                    if (ex is ApnsNotificationException)
                    {
                        var notificationException = (ApnsNotificationException) ex;


                        // Deal with the failed notification
                        var apnsNotification = notificationException.Notification;
                        var statusCode = notificationException.ErrorStatusCode;


                        _log.ErrorFormat($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
                    }
                    else
                    {
                        // Inner exception might hold more useful information like an ApnsConnectionException			
                        _log.ErrorFormat($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
                    }


                    // Mark it as handled
                    return true;
                });
            };


            apnsBroker.OnNotificationSucceeded += (notification) =>
            {
                _log.InfoFormat("Apple Notification Sent!");
            };


            // Start the broker
            apnsBroker.Start();




            // Queue a notification to send
            apnsBroker.QueueNotification(new ApnsNotification
            {
                DeviceToken = "58AA314A317D72AE83413F9C9FA6201D2573FAE472A40D72D0E528E7D0F51DED",
                Payload = JObject.Parse("{" +
                                        "\"aps\":{\"alert\":\"Hello PushSharp!\"}" +
                                        
                                        "}")
            });




            // Stop the broker, wait for it to finish   
            // This isn't done after every message, but after you're
            // done with the broker
            apnsBroker.Stop();


...


相关文章:

  • MySQL数据库灾难恢复
  • 使用C# Diagnostics.DebuggerDisplay属性提高调试信息可读性
  • PDI简单介绍之ETL工具-----spoon
  • python flask web环境配置步骤
  • python flask SQLAlchemy 的用法
  • Blog须知
  • Azure VM Scalable Sets -- 适用IAAS架构
  • 数据库设计技巧14则
  • 一张图说明HTTPS 握手过程
  • linux-2.6.26内核中ARM中断实现详解(1)
  • 创建Azure scheduler完成日常任务
  • (TipsTricks)用客户端模板精简JavaScript代码
  • eclipse 生成发布的apk (signed zipalign过程)
  • 使用黄金分割比值计算斐波那契数列
  • 遭遇飞信“中奖” 之 图文全攻略
  • 【5+】跨webview多页面 触发事件(二)
  • 【刷算法】求1+2+3+...+n
  • FastReport在线报表设计器工作原理
  • If…else
  • Java超时控制的实现
  • JSDuck 与 AngularJS 融合技巧
  • k个最大的数及变种小结
  • Node.js 新计划:使用 V8 snapshot 将启动速度提升 8 倍
  • Sass 快速入门教程
  • session共享问题解决方案
  • SpriteKit 技巧之添加背景图片
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 检测对象或数组
  • 解决iview多表头动态更改列元素发生的错误
  • 经典排序算法及其 Java 实现
  • 小程序测试方案初探
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • #我与Java虚拟机的故事#连载07:我放弃了对JVM的进一步学习
  • (2.2w字)前端单元测试之Jest详解篇
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (C语言)二分查找 超详细
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (原創) 如何安裝Linux版本的Quartus II? (SOC) (Quartus II) (Linux) (RedHat) (VirtualBox)
  • (转载)从 Java 代码到 Java 堆
  • (轉貼) 寄發紅帖基本原則(教育部禮儀司頒布) (雜項)
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET CLR基本术语
  • .NET Core使用NPOI导出复杂,美观的Excel详解
  • .NET MVC之AOP
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .Net6 Api Swagger配置
  • .NET6使用MiniExcel根据数据源横向导出头部标题及数据
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .net操作Excel出错解决
  • .net中的Queue和Stack
  • @TableId注解详细介绍 mybaits 实体类主键注解
  • [ 数据结构 - C++]红黑树RBTree