Swift ios10 遠(yuǎn)程推送

首先得配置一系列證書古毛,參考這篇文章用 Swift 實(shí)現(xiàn)通知推送的新手指南

這里得提一下,花了我很多時(shí)間的:必須保證鑰匙串中只有一個(gè)push的證書,可能是我測(cè)試中搞了好幾次,.p12文件一直沒有導(dǎo)出來(lái)汉操,另外沒法導(dǎo)出.p12文件的朋友請(qǐng)確定push證書在登錄模塊中,然后點(diǎn)我的證書蒙兰,選定證書導(dǎo)出磷瘤,如下圖


我用的推送服務(wù)器是這個(gè)

記得把代碼中的token復(fù)制到pusher中芒篷,然后選擇對(duì)應(yīng)的證書,如圖


結(jié)果如圖




////? AppDelegate.swift//? PushTest////? Created by 黃之信 on 17/3/13.//? Copyright ? 2017年 MichaelHuang. All rights reserved.//import UIKitimport UserNotifications@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {? ? var window: UIWindow?? ? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {? ? ? ? // Override point for customization after application launch.? ? ? ? registerAppNotificationSettings(launchOptions: launchOptions as [NSObject : AnyObject]?)? ? ? ? return true? ? }? ? private func registerAppNotificationSettings(launchOptions: [NSObject: AnyObject]?) {? ? ? ? if #available(iOS 10.0, *) {? ? ? ? ? ? let notifiCenter = UNUserNotificationCenter.current()? ? ? ? ? ? notifiCenter.delegate = self? ? ? ? ? ? let types = UNAuthorizationOptions(arrayLiteral: [.alert, .badge, .sound])? ? ? ? ? ? notifiCenter.requestAuthorization(options: types) { (flag, error) in? ? ? ? ? ? ? ? if flag {? ? ? ? ? ? ? ? ? ? print("iOS request notification success")? ? ? ? ? ? ? ? }else{? ? ? ? ? ? ? ? ? ? print(" iOS 10 request notification fail")? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? } else { //iOS8,iOS9注冊(cè)通知//? ? ? ? ? ? let setting = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)//? ? ? ? ? ? UIApplication.sharedApplication().registerUserNotificationSettings(setting)? ? ? ? }? ? ? ? ? ? ? ? UIApplication.shared.registerForRemoteNotifications()? ? }? ? ? ? func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {? ? ? ? ? ? ? ? var token: String = ""? ? ? ? for i in 0..Void){

let userInfo = notification.request.content.userInfo

print("userInfo10:\(userInfo)")

completionHandler([.sound,.alert])

}

//iOS10新增:處理后臺(tái)點(diǎn)擊通知的代理方法

@available(iOS 10.0, *)

func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void){

let userInfo = response.notification.request.content.userInfo

print("userInfo10:\(userInfo)")

completionHandler()

}

func applicationWillResignActive(_ application: UIApplication) {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

func applicationDidEnterBackground(_ application: UIApplication) {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

func applicationWillEnterForeground(_ application: UIApplication) {

// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

}

func applicationDidBecomeActive(_ application: UIApplication) {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

func applicationWillTerminate(_ application: UIApplication) {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末采缚,一起剝皮案震驚了整個(gè)濱河市针炉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仰担,老刑警劉巖糊识,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異摔蓝,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)愉耙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門贮尉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人朴沿,你說(shuō)我怎么就攤上這事猜谚。” “怎么了赌渣?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵魏铅,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我坚芜,道長(zhǎng)览芳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任鸿竖,我火速辦了婚禮沧竟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘缚忧。我一直安慰自己悟泵,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布闪水。 她就那樣靜靜地躺著糕非,像睡著了一般。 火紅的嫁衣襯著肌膚如雪球榆。 梳的紋絲不亂的頭發(fā)上朽肥,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音芜果,去河邊找鬼鞠呈。 笑死,一個(gè)胖子當(dāng)著我的面吹牛右钾,可吹牛的內(nèi)容都是我干的蚁吝。 我是一名探鬼主播旱爆,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼窘茁!你這毒婦竟也來(lái)了怀伦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤山林,失蹤者是張志新(化名)和其女友劉穎房待,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體驼抹,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡桑孩,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了框冀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片流椒。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖明也,靈堂內(nèi)的尸體忽然破棺而出宣虾,到底是詐尸還是另有隱情,我是刑警寧澤温数,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布绣硝,位于F島的核電站,受9級(jí)特大地震影響撑刺,放射性物質(zhì)發(fā)生泄漏鹉胖。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一猜煮、第九天 我趴在偏房一處隱蔽的房頂上張望次员。 院中可真熱鬧,春花似錦王带、人聲如沸淑蔚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)刹衫。三九已至,卻和暖如春搞挣,著一層夾襖步出監(jiān)牢的瞬間带迟,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工囱桨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留仓犬,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓舍肠,卻偏偏與公主長(zhǎng)得像搀继,于是被迫代替她去往敵國(guó)和親窘面。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容