百度推送--IOS(一)

来源:互联网 发布:阿桑奇曝光中国知乎 编辑:程序博客网 时间:2024/05/16 08:22

1、  百度推送封装了苹果的APNS和google的SMS推送,作为开发者可以通过百度来完成这两个平台的推送。

百度推送开发指南:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/guideios

  苹果APNS开发相关指南请参考苹果开发者网站:https://developer.apple.com/

本文是完成简单的功能,可以发送通知到客户端,之后我会将这部分内容写成一个phonegap插件,以后在做推送时直接调用就可以了。

2、相关准备工作

在此之前,需要进入百度云推送管理控制台→开发者服务→云推送→推送设置,  新建一个工程,百度回返回给所建工程的的一些信息:

3、新建一个工程,导入第三方的包和头文件;

4、新建一个Property List 文件将百度返回给我们的工程信息添进去;


5、将导入的文件修改为非ARC模式:


6、打开AppDelegate.m引用头文件同时申请要用到的变量:

[objc] view plaincopy
  1. #import "MainViewController.h"  
  2. #import "BPush.h"  
  3. #import "JSONKit.h"  
  4. #import <Cordova/CDVPlugin.h>  
[objc] view plaincopy
  1. @interface AppDelegate ()  
  2.   
  3. @property (nonatomicretainNSString *appId;  
  4. @property (nonatomicretainNSString *channelId;  
  5. @property (nonatomicretainNSString *userId;  
  6.   
  7.   
  8. @end  
  9.   
  10. @implementation AppDelegate  
  11.   
  12. @synthesize window, viewController;  
  13.   
  14. @synthesize appId, channelId, userId;  


7、注册push

[objc] view plaincopy
  1. - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions  
  2. {  
  3. self.window.rootViewController = self.viewController;  
  4.     [self.window makeKeyAndVisible];  
  5.   
  6.     //初始化push  
  7.     [BPush setupChannel:launchOptions];  
  8.     [BPush setDelegate:self];  
  9.     //在程序图标的右上角显示数字  
  10.     [application setApplicationIconBadgeNumber:0];  
  11.     //管理远程通知  方法来与苹果推送通知服务注册  
  12.     [application registerForRemoteNotificationTypes:  
  13.      /*推送的类型*/  
  14.      UIRemoteNotificationTypeAlert  
  15.      | UIRemoteNotificationTypeBadge  
  16.      | UIRemoteNotificationTypeSound];  
  17.   
  18.     NSLog(@"++++++++++++++++++++++");  
  19.       
  20.     return YES;  
  21.   
  22. }  

8、因为在推送时开发者需要知道客户端手机的userid 和 channelid,所以我们需要在程序中获取到这些信息:

[objc] view plaincopy
  1. - (void) onMethod:(NSString*)method response:(NSDictionary*)data {  
  2.     NSLog(@"On method:%@", method);  
  3.     NSLog(@"data:%@", [data description]);  
  4.     NSDictionary* res = [[NSDictionary alloc] initWithDictionary:data];  
  5.       
  6.     if ([BPushRequestMethod_Bind isEqualToString:method]) {  
  7.         //从字典里取值  
  8.         NSString *appid = [res valueForKey:BPushRequestAppIdKey];  
  9.         NSString *userid = [res valueForKey:BPushRequestUserIdKey];  
  10.         NSString *channelid = [res valueForKey:BPushRequestChannelIdKey];  
  11.         NSString *requestid = [res valueForKey:BPushRequestRequestIdKey];  
  12.         int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];  
  13.           
  14.         if (returnCode == BPushErrorCode_Success) {  
  15.            
  16.               
  17.             // 在内存中备份,以便短时间内进入可以看到这些值,而不需要重新bind  
  18.             self.appId = appid;  
  19.             self.channelId = channelid;  
  20.             self.userId = userid;  
  21.               
  22.             [[NSUserDefaults standardUserDefaults] setObject:userId  
  23.                                                       forKey:@"BAIDUNOTIFICATION_USERID"];  
  24.             [[NSUserDefaults standardUserDefaults] setObject:channelId  
  25.                                                       forKey:@"BAIDUNOTIFICATION_CHANNELID"];  
  26.               
  27.         }  
  28.     } else if ([BPushRequestMethod_Unbind isEqualToString:method]) {  
  29.         int returnCode = [[res valueForKey:BPushRequestErrorCodeKey] intValue];  
  30.         if (returnCode == BPushErrorCode_Success) {  
  31.               
  32.         }  
  33.     }  
  34.       
  35.     NSLog(@"%@===========%@==============",method,[data description]);  
  36.       
  37. }  
9、在客户端接收推送来的信息:

[objc] view plaincopy
  1. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
  2.   
  3.     NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];  
  4.     //程序运行的状态判断  
  5.     if (application.applicationState == UIApplicationStateActive) {  
  6.         // Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.  
  7.         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification"  
  8.                                                             message:[NSString stringWithFormat:@"The application received this remote notification while it was running:\n%@", alert]  
  9.                                                            delegate:self  
  10.                                                   cancelButtonTitle:@"OK"  
  11.                                                   otherButtonTitles:nil];  
  12.         [alertView show];  
  13.     }  
  14.     // 写程序图标左上角显示数字  
  15.     [application setApplicationIconBadgeNumber:0];  
  16.     //推送反馈  
  17.     [BPush handleNotification:userInfo];  
  18.         NSLog(@"%@",[userInfo JSONString]);  
  19.         NSLog(@"didReceiveRemoteNotification=======%@\n=========%@\n========%@\n",self.appId,self.userId,self.channelId);  
  20. }  
11、推送之后,由客户端返回推送是成功还是失败:

[objc] view plaincopy
  1. - (void)application:(UIApplication *)application  
  2.    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  3. {  
  4.      
  5.     [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:token];  
  6.     [BPush registerDeviceToken:deviceToken];  
  7.     [BPush bindChannel];  
  8.     NSLog(@"%@", deviceToken);  
  9. }  
[objc] view plaincopy
  1. //失败  
  2. - (void)                                 application:(UIApplication *)application  
  3.     didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  
  4. {  
  5.        // re-post ( broadcast )  
  6.     [[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotificationError object:error];  
  7.       
  8.     NSLog(@"Error in registration. Error: %@", error);  
  9.       
  10. }  
12、现在基本的代码工作已经完成,在我们运行程之前需要把我们的推送证书由p12格式转换成pem格式,证书转换具体方法请参考苹果官网:


13、Ok,在真机上运行我们的工程,这是获取到得手机的数据:



14、打开百度开发者服务→云推送→通知 ,选择添加通知推送,这样在客户端有网的情况下就可以将通知发送到客户端上。

15、demo下载请戳:https://github.com/dengfeng520/GSbaidupush

0 0
原创粉丝点击