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();


...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值