证书的配置方面,我就不多说了,严格按照SDK上的步骤,一步一步的认认真真的配置证书。
下面我来说一下在APPDelegate里面需要写的代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//注册服务器推送,请求用户授权
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 测试 开发环境 时需要修改BPushMode为BPushModeDevelopment 需要修改Apikey为自己的Apikey
// 在 App 启动时注册百度云推送服务,需要提供 Apikey
BPush.registerChannel(launchOptions, apiKey: "XXXXXXXXXXX", pushMode: BPushMode.Development, withFirstAction: nil, withSecondAction: nil, withCategory: nil, isDebug: true)
// App 是用户点击推送消息启动
if (launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] != nil) {
let userInfo = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary
if ((userInfo) != nil) {
BPush.handleNotification(userInfo as! [NSObject : AnyObject])
}
}
//角标清0
let shared = UIApplication.sharedApplication()
shared.applicationIconBadgeNumber = 0
//设置状态栏的字体颜色模式
shared.statusBarStyle = UIStatusBarStyle.LightContent
self.window?.makeKeyAndVisible()
return true
}
// 远程推送通知 注册成功
func application(application: UIApplication , didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData ) {
// 向云推送注册 device token
BPush.registerDeviceToken(deviceToken)
// 绑定channel.将会在回调中看获得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]];
// 需要在绑定成功后进行 settag listtag deletetag unbind 操作否则会失败
if ((result) != nil){
BPush.setTag("Mytag", withCompleteHandler: { (result, error) -> Void in
if ((result) != nil){
}
})
}
})
}
// 当 DeviceToken 获取失败时,系统会回调此方法
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
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationState.Active || application.applicationState == UIApplicationState.Background) {
let alertView = UIAlertView (title: "收到一条消息", message: alertDic, delegate: nil , cancelButtonTitle: " 取消 ",otherButtonTitles:"确定")
alertView.show ()
}
else//杀死状态下,直接跳转到跳转页面。
{
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("finish")
// 根视图是普通的viewctr 用present跳转
let tabBarCtr = self.window?.rootViewController
tabBarCtr?.presentViewController(vc, animated: true, completion: nil)
}
}
// 此方法是 用户点击了通知,应用在前台 或者开启后台并且应用在后台 时调起
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
// 应用在前台 或者后台开启状态下,不跳转页面,让用户选择。
if (application.applicationState == UIApplicationState.Active || application.applicationState == UIApplicationState.Background) {
let alertView = UIAlertView (title: "收到一条消息", message: alertDic, delegate: nil , cancelButtonTitle: " 取消 ",otherButtonTitles:"确定")
alertView.show ()
}
else//杀死状态下,直接跳转到跳转页面。
{
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("finish")
// 根视图是普通的viewctr 用present跳转
let tabBarCtr = self.window?.rootViewController
tabBarCtr?.presentViewController(vc, animated: true, completion: nil)
}
}
// 注册通知 alert 、 sound 、 badge ( 8.0 之后,必须要添加下面这段代码,否则注册失败)
func application(application: UIApplication , didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings ) {
application.registerForRemoteNotifications ()
}