注:可參考百度推送IOS用戶手冊,控制檯管理請參考百度推送IOS集成指南ios
版權全部,轉載請註明出處,謝謝!api
打開百度提供的SDK包,找到LibBPush文件夾,打開。刪掉opensource文件夾裏面的JSONKit.h和JSONKit.m(由於kony編譯的IOS工程已經包含了這個類)app
2. 刪除了JSONKit這個類文件,對應的要刪除其餘文件(BpushClass.m)中引用了這個類的語句(#import 「JSONKit.h」)框架
3. 修改一下幾個地方的代碼:dom
在OpenUDID.miphone
註釋下面的代碼ide
//static NSString * const kOpenUDIDDescription = @"OpenUDID_with_iOS6_Support"; 修改如下代碼 CC_MD5( cStr, strlen(cStr), result ); 修改成 CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08x" 修改成 @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%08lx" (NSUInteger)(arc4random() % NSUIntegerMax)]; 修改成 (unsigned long)(arc4random() % NSUIntegerMax)];
4. 建立並配置 BPushConfig.plist 文件(能夠直接導出百度提供Demo下的plist文件,在工程下拷貝出來便可)atom
在工程中建立一個新的 Property List 文件,並命名爲 BPushConfig.plist,添加如下鍵值:spa
「API_KEY」 = 「pDUCHGTbD346jt2klpHRjHp7」 「DEBUG」 = NO 「BPUSH_CHANNEL」 = 「91」
API_KEY:必選。百度開發者中心爲每一個 app 自動分配的 api key,在開發者中心 app 基本信息中能夠查看。調試
PRODUCTION_MODE:必選。應用發佈模式。開發證書籤名時,值設爲」NO」;發佈證書籤名時,值設爲」YES」。請在調試和發佈應用時,修改正確設置這個值,以避免出現推送通知沒法到達。
DEBUG:可選。Push SDK 調試模式開關,值爲 YES 時,將打開 SDK 日誌。
BPUSH_CHANNEL:可選。渠道號,雲推送將會進行統計,在控制檯能夠看到統計結果。
5.下面集成最關鍵的代碼:
在Xcode下新建一個類,這兒以iosbdpush類爲例。
在iosbdpush.h中添加如下代碼:
注意:iosbdpush類繼承自NSObject → @interfa iosbdpush : NSObject
@property (strong, nonatomic) NSString *appId; @property (strong, nonatomic) NSString *channelId; @property (strong, nonatomic) NSString *userId; +(id)shareInstance; -(void)initBPush; -(NSString *) getUserId_kony; -(NSString *) getChannelId_kony; -(void)setDeviceToken:(NSString *)deviceToken; -(void)setPushAPIKey:(NSString *)apikey;
在iosbdpush.m中添加如下代碼:
#import "BPush.h" #import "OpenUDID.h" @implementation iosbdpush @synthesize appId, channelId, userId; +(id)shareInstance { return [[iosbdpush alloc] init]; } -(void)initBPush { NSLog(@"=======initBPush======"); [BPush setupChannel:nil]; // 必須 iosbdpush *obj = [[iosbdpush alloc] init]; [BPush setDelegate:obj]; // 必須。參數對象必須實現onMethod: response:方法,本示例中爲self //[BPush bindChannel]; // [BPush setAccessToken:@"3.ad0c16fa2c6aa378f450f54adb08039.2592000.1367133742.282335-602025"]; // 可選。api key綁定時不須要,也可在其它時機調用 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; NSLog(@"=========== phoneVersion:%@",phoneVersion); if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { UIUserNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; UIApplication *application = [UIApplication sharedApplication]; [application registerForRemoteNotifications]; }else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; } } -(NSString *) getUserId_kony { return [BPush getUserId]; } -(NSString *) getChannelId_kony { return [BPush getChannelId]; } -(void)setDeviceToken:(NSString *)deviceToken { NSString *hexToken = [NSString stringWithFormat:@"<%@>",deviceToken]; NSData* deviceTokenData = [self dataFromHexString:hexToken]; NSLog(@"deviceTokenData NSData:%@",deviceTokenData); [BPush registerDeviceToken:deviceTokenData]; // 必須 [BPush bindChannel]; // 必須。能夠在其它時機調用,只有在該方法返回(經過onMethod:response:回調)綁定成功時,app才能接收到Push消息。一個app綁定成功至少一次便可(若是access token變動請從新綁定)。 } // 必須,若是正確調用了setDelegate,在bindChannel以後,結果在這個回調中返回。 // 若綁定失敗,請進行從新綁定,確保至少綁定成功一次 - (void) onMethod:(NSString*)method response:(NSDictionary*)data { NSLog(@"On method:%@", method); NSLog(@"data:%@", [data description]); NSDictionary* res = [[[NSDictionary alloc] initWithDictionary:data] autorelease]; if ([BPushRequestMethod_Bind isEqualToString:method]) { NSString *appid = [res valueForKey:BPushRequestAppIdKey]; NSString *userid = [res valueForKey:BPushRequestUserIdKey]; NSString *channelid = [res valueForKey:BPushRequestChannelIdKey]; NSLog(@"appid--userid--channelid==%@ %@ %@",appid, userid, channelid); } } //轉換device token -(NSData *) dataFromHexString:(NSString *) hexstr { NSMutableData *data = [[NSMutableData alloc] init]; NSString *inputStr = [hexstr uppercaseString]; NSString *hexChars = @"0123456789ABCDEF"; Byte b1,b2; b1 = 255; b2 = 255; for (int i=0; i<hexstr.length; i++) { NSString *subStr = [inputStr substringWithRange:NSMakeRange(i, 1)]; NSRange loc = [hexChars rangeOfString:subStr]; if (loc.location == NSNotFound) continue; if (255 == b1) { b1 = (Byte)loc.location; }else { b2 = (Byte)loc.location; //Appending the Byte to NSData Byte *bytes = malloc(sizeof(Byte) *1); bytes[0] = ((b1<<4) & 0xf0) | (b2 & 0x0f); [data appendBytes:bytes length:1]; b1 = b2 = 255; } } return data; }
6.整合推送包文件。
把上面新建的BPushConfig.plist、iosbdpush.h、iosbdpush.m這三個文件拷貝到整理好的LibBPush文件夾下面
而後在Windows下面打包爲 .zip(打包後的名稱爲:LibPush.zip)
注意:必定要在window下打包,mac下打包有問題,且格式必須爲zip,mac下打包後在window下解壓,你會發現多出一個名爲__MACOSX的文件夾來,如:
7. 如今開始往kony上集成
在kony→工程右鍵→Integrate Third Party→Manage Custom Libraries
添加一個命名空間,以iosbdPush爲例
添加一個類
添加類的詳細
上圖中的Frameworks:蘋果引用的第三方框架,百度推送須要用到Foundation.framework、CoreTelephony.framework、SystemConfiguration.framework、libz.dylib這四個框架,但咱們的工程已經包含了其中三個,因此只需添加CoreTelephony.framework這一個,若是要添加多個框架,可用英文逗號「,」隔開
Class:就是前面咱們本身建立的類,示例建立的類名是iosbdpush
Method:靜態方法,參考iosbdpush.m中的 +(id)shareInstance
下面添加方法:
這兒只是說明添加帶參數和布帶參數的方法時須要注意的細節,請看下圖:
8. 下面開始在kony裏面調用剛剛集成的第三方包
在kony工程裏面新建一個js文件,添加以下代碼:
//初始化百度推送 function iosbdPushinit(){ if(appConstans.getPlatform()=="iphone"){ var iosbdPushObject = new iosbdPush.iosbdPush(); //Invokes method 'initBdPush' on the object iosbdPushObject.initBdPush(); } } //獲取設備的UserID和ChannelID function iosGetUserID(){ if(appConstans.getPlatform()=="iphone"){ //Creates an object of class 'iosbdPush' var iosbdPushObject = new iosbdPush.iosbdPush(); //Invokes method 'getUserID' on the object appConstans.bdPushUserID = iosbdPushObject.getUserID(); appConstans.bdPushChannelID = iosbdPushObject.getChannelID(); kony.print("bdPushUserID :: "+appConstans.bdPushUserID); kony.print("bdPushChannelID :: "+appConstans.bdPushChannelID); } } function oniospushsuccess(identifier){ if(appConstans.getPlatform()=="iphone"){ kony.print("oniospushsuccess Registered SUCCESSFULLY :"+identifier); var iosbdPushObject = new iosbdPush.iosbdPush(); //Invokes method 'setIosDeviceToken' on the object iosbdPushObject.setIosDeviceToken(identifier); } } function oniospushfailure(errortable){ kony.print("+++++oniospushfailure Registration Failed :"+JSON.stringify(errortable)); } function onlineiospushCallback(msg){ if(appConstans.getPlatform()=="iphone"){ kony.print("onlineiospushCallback online:"+JSON.stringify(msg)); //var Jmsg = JSON.stringify(msg); kony.print("msg.title"+msg.title); kony.print("msg.msgId"+msg.msgId); kony.print("msg.description"+msg.description); appConstans.pushMsgID = msg.msgId; if(loginStatus){ pushMsgPop.lblTitle.text = msg.title; pushMsgPop.lblDescribe.text = msg.description; pushMsgPop.show(); }else{ frmLogin.show(); } } } function offlineiospushCallback(msg){ kony.print("offlineiospushCallback offline:"+JSON.stringify(msg)); } function oniospushderegsuccess(){ kony.print("oniospushderegsuccess Deregistered Successfully :"); alert("oniospushderegsuccess=="+msg); } function oniospushderegfailure(errortable){ kony.print("oniospushderegfailure Deregistration Failed"); alert("oniospushderegfailure=="+msg); } //註冊設備 function iosdevicepushInit(){ kony.print("iospushInit begin"); var Object = { onsuccessfulregistration:oniospushsuccess ,//註冊成功 onfailureregistration:oniospushfailure , //註冊失敗 onlinenotification:onlineiospushCallback ,//應用這在運行 offlinenotification:offlineiospushCallback ,//應用未運行或後臺掛起狀態 onsuccessfulderegistration:oniospushderegsuccess , onfailurederegistration:oniospushderegfailure }; if(appConstans.getPlatform()=="iphone"){ kony.push.setCallbacks(Object); var config=[0,1,2]; kony.push.register(config); } kony.print("iospushInit end"); } 集成到此就OK了,不明白的地方能夠百度,也能夠加我QQ或者留言交流!若有錯誤歡迎你們指正。。 本帖只作技術交流~~~!