swift接入信鸽推送

代码来自:https://segmentfault.com/a/1190000003736622

//
//  XinGeAppDelegate.swift
//  XinGeSwiftDemo12
//
//  Created by 张青明 on 15/8/27.
//  copyright (c) 2015年 极客栈. All rights reserved.
//

import UIKit


let IPHONE_8:Int32 = 80000


/// ACCESS ID
let kXinGeAppId: UInt32 = 2200207974//填写ACCESS ID,例如:1234567890

/// ACCESS KEY
let kXinGeAppKey:String! = "I4UAIU9117TJ"//填写ACCESS KEY,例如:"AB345F7H89012"

class XinGeAppDelegate: UIResponder,UIApplicationDelegate {
    
    
    
    func registerPushForIOS8()
    {
        //Types
        let types = UIUserNotificationType.sound// | UIUserNotificationType.Badge | UIUserNotificationType.sound
        
        //Actions
        let acceptAction = UIMutableuserNotificationAction()
        
        acceptAction.identifier = "ACCEPT_IDENTIFIER"
        acceptAction.title      = "Accept"
        
        acceptAction.activationMode = UIUserNotificationActivationMode.Foreground
        
        acceptAction.destructive = false
        acceptAction.authenticationrequired = false
        
        
        //Categories
        let inviteCategory = UIMutableuserNotificationCategory()
        inviteCategory.identifier = "INVITE_CATEGORY";
        
        inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Default)
        inviteCategory.setActions([acceptAction],forContext: UIUserNotificationActionContext.Minimal)
        
        //let categories = NSSet(objects: inviteCategory)
        let categories = Set(arrayLiteral: inviteCategory)
        
        
        let mySettings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types,categories: categories)
        
        UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
        
    }
    
    func registerPush()
    {
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(UIRemoteNotificationType.sound)
    }
    
    func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        // 注册
        XGPush.startApp(kXinGeAppId,appKey: kXinGeAppKey)
        
        XGPush.initForReregister { () -> Void in
            //如果变成需要注册状态
            if !XGPush.isUnRegisterStatus()
            {
                
                if __IPHONE_OS_VERSION_MAX_ALLOWED >= IPHONE_8
                {
                    
                    if (UIDevice.currentDevice().systemVersion.compare("8",options:.NumericSearch) != NSComparisonResult.OrderedAscending)
                    {
                        self.registerPushForIOS8()
                    }
                    else
                    {
                        self.registerPush()
                    }
                    
                }
                else
                {
                    //iOS8之前注册push方法
                    //注册Push服务,注册后才能收到推送
                    self.registerPush()
                }
                
                
            }
        }
        
//        XGPush.clearlocalnotifications()
        
        
        XGPush.handleLaunching(launchOptions,successCallback: { () -> Void in
            print("[XGPush]handleLaunching's successBlock\n\n")
            }) { () -> Void in
                print("[XGPush]handleLaunching's errorBlock\n\n")
        }
        return true
    }
    
    
    func application(application: UIApplication,didReceivelocalnotification notification: UIlocalnotification) {
        XGPush.localnotificationAtFrontEnd(notification,userInfoKey: "clockID",userInfovalue: "myid")
        
        XGPush.dellocalnotification(notification)
    }
    
    
    @available(iOS,introduced=8.0)
    func application(application: UIApplication,didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        UIApplication.sharedApplication().registerForRemoteNotifications()
    }
    @available(iOS,handleActionWithIdentifier identifier: String?,forRemoteNotification userInfo: [NSObject : AnyObject],completionHandler: () -> Void) {
        
        if let ident = identifier
        {
            if ident == "ACCEPT_IDENTIFIER"
            {
                print("ACCEPT_IDENTIFIER is clicked\n\n")
            }
        }
        
        completionHandler()
    }
    
    
    
    func application(application: UIApplication,didRegisterForRemoteNotificationsWithDevicetoken devicetoken: NSData) {
        // 设置账号
        //	[XGPush setAccount:@"test"];
        XGPush.setAccount("372627230")
        //注册设备
        //        XGSetting.getInstance().Channel = ""//= "appstore"
        //        XGSetting.getInstance().GameServer = "家万户"
        
        let devicetokenStr = XGPush.registerDevice(devicetoken,successCallback: { () -> Void in
            print("[XGPush]register successBlock\n\n")
            }) { () -> Void in
                print("[XGPush]register errorBlock\n\n")
        }
        
        print("devicetokenStr:\(devicetokenStr)\n\n")
    }
    
    
    func application(application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print("didFailToRegisterForRemoteNotifications error:\(error.localizedDescription)\n\n")
    }
    
    // iOS 3 以上
    func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        
//        UIAlertView(title: "3-",message: "didReceive",delegate: self,cancelButtonTitle: "OK").show()
        let apsDictionary = userInfo["aps"] as? NSDictionary
        if let apsDict = apsDictionary
        {
            let alertView = UIAlertView(title: "您有新的消息",message: apsDict["alert"] as? String,cancelButtonTitle: "确定")
            alertView.show()
        }
        
        // 清空通知栏通知
        XGPush.clearlocalnotifications()
        UIApplication.sharedApplication().cancelAlllocalnotifications()
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        
        XGPush.handleReceiveNotification(userInfo)
    }
    
    // iOS 7 以上
    func application(application: UIApplication,didReceiveRemoteNotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)
    {
//        UIAlertView(title: "7-",cancelButtonTitle: "OK").show()
        print("响应了")
        let apsDictionary = userInfo["aps"] as? NSDictionary
        if let apsDict = apsDictionary
        {
            let alertView = UIAlertView(title: "您有新的消息",cancelButtonTitle: "确定")
            alertView.show()
        }
        // 清空通知栏通知
        XGPush.clearlocalnotifications()
        UIApplication.sharedApplication().cancelAlllocalnotifications()
        UIApplication.sharedApplication().applicationIconBadgeNumber = 0
        
        XGPush.handleReceiveNotification(userInfo)
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...