加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift接入信鸽推送

发布时间:2020-12-14 06:51:34 所属栏目:百科 来源:网络整理
导读:代码来自:https://segmentfault.com/a/1190000003736622 //// XinGeAppDelegate.swift// XinGeSwiftDemo12//// Created by 张青明 on 15/8/27.// Copyright (c) 2015年 极客栈. All rights reserved.//import UIKitlet IPHONE_8:Int32 = 80000/// ACCESS ID

代码来自: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 successBlocknn")
            }) { () -> Void in
                print("[XGPush]handleLaunching's errorBlocknn")
        }
        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 clickednn")
            }
        }
        
        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 successBlocknn")
            }) { () -> Void in
                print("[XGPush]register errorBlocknn")
        }
        
        print("deviceTokenStr:(deviceTokenStr)nn")
    }
    
    
    func application(application: UIApplication,didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print("didFailToRegisterForRemoteNotifications error:(error.localizedDescription)nn")
    }
    
    // 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)
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读