项目中运用百度云推送(一)

1.  在#import"AppDelegate.m"添加

2.

3.

4.

5.

6.

- (void)application:(UIApplication*)application didRegisterUserNotificationSettings:(UIUserNotificationSettings*)notificationSettings {

[applicationregisterForRemoteNotifications];

}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {

NSLog(@"test:%@",deviceToken);

[BPushregisterDeviceToken: deviceToken];

[BPushbindChannelWithCompleteHandler:^(idresult,NSError*error) {

//[self addLogString:[NSString stringWithFormat:@"Method: %@\n%@",BPushRequestMethodBind,result]];

if(result) {

[BPushsetTag:@"Mytag"withCompleteHandler:^(idresult,NSError*error) {

if(result) {

NSLog(@"设置tag成功");

}

}];

}

}];

//[BPush bindChannel];

}

//当DeviceToken获取失败时,系统会回调此方法

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

NSLog(@"DeviceToken获取失败,原因:%@",error);

}

- (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification {

if(notification.userInfo!=nil) {

if(isUserSelectNotification) {

[selfaddMessageFromRemoteNotification:notification.userInfoupdateUI:NO];

[[UIApplicationsharedApplication]cancelLocalNotification:notification];

}else{

isUserSelectNotification=YES;

}

}

}

//App状态为正在前台或者后台运行,那么此函数将被调用

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {

NSLog(@"Receive Notify: %@", [userInfoJSONString]);

isUserSelectNotification=NO;

if(application.applicationState==UIApplicationStateActive) {

//1.创建本地通知的对象

UILocalNotification*notification = [[UILocalNotificationalloc]init];

//2.设置通知触发的时间点

notification.fireDate= [NSDatedate];

//3.设置弹出提示的内容

notification.alertBody= [[userInfoobjectForKey:@"aps"]objectForKey:@"alert"];

notification.userInfo= userInfo;

//4.提示按钮的标题

notification.alertAction= [userInfoobjectForKey:@"关闭"];

notification.soundName=UILocalNotificationDefaultSoundName;

//注册本地通知,注册之后,通知才会生效

[[UIApplicationsharedApplication]scheduleLocalNotification:notification];

//设置应用程序图标上的数值

//[UIApplication sharedApplication].applicationIconBadgeNumber = userInfo.count;

}else{

[selfaddMessageFromRemoteNotification:userInfoupdateUI:NO];

}

application.applicationIconBadgeNumber=0;

AudioServicesPlaySystemSound(1008);

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

[BPushhandleNotification:userInfo];

}

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

isUserSelectNotification=NO;

if(application.applicationState==UIApplicationStateActive) {

//1.创建本地通知的对象

UILocalNotification*notification = [[UILocalNotificationalloc]init];

//2.设置通知触发的时间点

notification.fireDate= [NSDatedate];

//3.设置弹出提示的内容

notification.alertBody= [[userInfoobjectForKey:@"aps"]objectForKey:@"alert"];

notification.userInfo= userInfo;

//4.提示按钮的标题

notification.alertAction= [userInfoobjectForKey:@"关闭"];

notification.soundName=UILocalNotificationDefaultSoundName;

//注册本地通知,注册之后,通知才会生效

[[UIApplicationsharedApplication]scheduleLocalNotification:notification];

//设置应用程序图标上的数值

//[UIApplication sharedApplication].applicationIconBadgeNumber = userInfo.count;

}else{

[selfaddMessageFromRemoteNotification:userInfoupdateUI:NO];

}

application.applicationIconBadgeNumber=0;

AudioServicesPlaySystemSound(1008);

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

[BPushhandleNotification:userInfo];

}

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

UIAlertView*al = [[UIAlertViewalloc]initWithTitle:[BPushgetChannelId]message:[BPushgetUserId]delegate:nilcancelButtonTitle:nilotherButtonTitles:@"OK",nil];

[alshow];

}

- (void)addMessageFromRemoteNotification:(NSDictionary*)userInfo updateUI:(BOOL)updateUI {

[[NSNotificationCenterdefaultCenter]postNotificationName:@"PresentView"object:selfuserInfo:userInfo];

}

//独立客户端回调函数

//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

//

//[self parse:url application:application];

//return YES;

//}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容