代码来自: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 IDlet kXinGeAppID: UInt32 = 2200207974//填写ACCESS ID,例如:1234567890/// ACCESS KEYlet 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) }}总结
以上是内存溢出为你收集整理的swift接入信鸽推送全部内容,希望文章能够帮你解决swift接入信鸽推送所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)