IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    csharp25发表于 2017-05-18 23:03:14
    love 0
    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();
    
    
    ...




沪ICP备19023445号-2号
友情链接