證書的配置方面灭将,我就不多說了损同,嚴(yán)格按照SDK上的步驟粥脚,一步一步的認(rèn)認(rèn)真真的配置證書纵揍。
下面我來說一下在APPDelegate里面需要寫的代碼:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//注冊(cè)服務(wù)器推送,請(qǐng)求用戶授權(quán)
let version:String = UIDevice.currentDevice().systemVersion
// iOS8 下需要使用新的 API
if (version as NSString).floatValue >= 8.0{
let Types = UIUserNotificationType([.Sound,.Alert, .Badge])
let settings = UIUserNotificationSettings(forTypes: Types, categories: nil)
application.registerUserNotificationSettings(settings)
}else{
let Types = UIRemoteNotificationType([.Sound,.Alert, .Badge])
application.registerForRemoteNotificationTypes(Types)
}
//放在登錄
if ( UIApplication.instancesRespondToSelector ( Selector ( "registerUserNotificationSettings:" ))) {
let Types = UIUserNotificationType([.Sound,.Alert, .Badge])
let settings = UIUserNotificationSettings(forTypes: Types, categories: nil)
application.registerUserNotificationSettings(settings)
} else {
let Types = UIRemoteNotificationType([.Sound,.Alert, .Badge])
application.registerForRemoteNotificationTypes(Types)
}
//#warning 測(cè)試 開發(fā)環(huán)境 時(shí)需要修改BPushMode為BPushModeDevelopment 需要修改Apikey為自己的Apikey
// 在 App 啟動(dòng)時(shí)注冊(cè)百度云推送服務(wù)垛玻,需要提供 Apikey
BPush.registerChannel(launchOptions, apiKey: "XXXXXXXXXXX", pushMode: BPushMode.Development, withFirstAction: nil, withSecondAction: nil, withCategory: nil, isDebug: true)
// App 是用戶點(diǎn)擊推送消息啟動(dòng)
if (launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
if ((userInfo) != nil) {
BPush.handleNotification(userInfo as! [NSObject : AnyObject])
}
}
//角標(biāo)清0
let shared = UIApplication.sharedApplication()
shared.applicationIconBadgeNumber = 0
//設(shè)置狀態(tài)欄的字體顏色模式
shared.statusBarStyle = UIStatusBarStyle.LightContent
self.window?.makeKeyAndVisible()
return true
}
// 遠(yuǎn)程推送通知 注冊(cè)成功
func application(application: UIApplication , didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
//? 向云推送注冊(cè) device token
BPush.registerDeviceToken(deviceToken)
// 綁定channel.將會(huì)在回調(diào)中看獲得channnelid appid userid 等
BPush.bindChannelWithCompleteHandler({ (result, error) -> Void in
let baiduUser = result["user_id"] as! String
let channelID = result["channel_id"] as! String
//? [self.viewController addLogString:[NSString stringWithFormat:@"Method: %@\n%@",BPushRequestMethodBind,result]];
// 需要在綁定成功后進(jìn)行 settag listtag deletetag unbind 操作否則會(huì)失敗
if ((result) != nil){
BPush.setTag("Mytag", withCompleteHandler: { (result, error) -> Void in
if ((result) != nil){
}
})
}
})
}
// 當(dāng) DeviceToken 獲取失敗時(shí)割捅,系統(tǒng)會(huì)回調(diào)此方法
func application(application: UIApplication , didFailToRegisterForRemoteNotificationsWithError error: NSError ) {
if error.code == 3010 {
} else {
}
}
func application(application: UIApplication , didReceiveRemoteNotification userInfo: [ NSObject : AnyObject ]) {
// App 收到推送的通知
BPush.handleNotification(userInfo as [NSObject : AnyObject]!)
let notif? ? = userInfo as NSDictionary
let apsDic? = notif.objectForKey ( "aps" ) as! NSDictionary
let alertDic = apsDic.objectForKey ( "alert" ) as! String
// 應(yīng)用在前臺(tái) 或者后臺(tái)開啟狀態(tài)下,不跳轉(zhuǎn)頁(yè)面帚桩,讓用戶選擇亿驾。
if (application.applicationState == UIApplicationState.Active || application.applicationState == UIApplicationState.Background) {
let alertView = UIAlertView (title: "收到一條消息", message: alertDic, delegate: nil , cancelButtonTitle: " 取消 ",otherButtonTitles:"確定")
alertView.show ()
}
else//殺死狀態(tài)下,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁(yè)面账嚎。
{
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("finish")
// 根視圖是普通的viewctr 用present跳轉(zhuǎn)
let tabBarCtr = self.window?.rootViewController
tabBarCtr?.presentViewController(vc, animated: true, completion: nil)
}
}
// 此方法是 用戶點(diǎn)擊了通知莫瞬,應(yīng)用在前臺(tái) 或者開啟后臺(tái)并且應(yīng)用在后臺(tái) 時(shí)調(diào)起
func application(application: UIApplication , didReceiveRemoteNotification userInfo: [ NSObject : AnyObject ], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult ) -> Void ) {
let notif? ? = userInfo as NSDictionary
let apsDic? = notif.objectForKey ( "aps" ) as! NSDictionary
let alertDic = apsDic.objectForKey ( "alert" ) as! String
// 應(yīng)用在前臺(tái) 或者后臺(tái)開啟狀態(tài)下,不跳轉(zhuǎn)頁(yè)面郭蕉,讓用戶選擇疼邀。
if (application.applicationState == UIApplicationState.Active || application.applicationState == UIApplicationState.Background) {
let alertView = UIAlertView (title: "收到一條消息", message: alertDic, delegate: nil , cancelButtonTitle: " 取消 ",otherButtonTitles:"確定")
alertView.show ()
}
else//殺死狀態(tài)下,直接跳轉(zhuǎn)到跳轉(zhuǎn)頁(yè)面召锈。
{
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("finish")
// 根視圖是普通的viewctr 用present跳轉(zhuǎn)
let tabBarCtr = self.window?.rootViewController
tabBarCtr?.presentViewController(vc, animated: true, completion: nil)
}
}
// 注冊(cè)通知 alert 旁振、 sound 、 badge ( 8.0 之后烟勋,必須要添加下面這段代碼规求,否則注冊(cè)失敗)
func application(application: UIApplication , didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings ) {
application.registerForRemoteNotifications ()
}