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(@"設(shè)置tag成功");
}
}];
}
}];
//[BPush bindChannel];
}
//當(dāng)DeviceToken獲取失敗時(shí)往踢,系統(tǒng)會(huì)回調(diào)此方法
- (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狀態(tài)為正在前臺(tái)或者后臺(tái)運(yùn)行悯嗓,那么此函數(shù)將被調(diào)用
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
NSLog(@"Receive Notify: %@", [userInfoJSONString]);
isUserSelectNotification=NO;
if(application.applicationState==UIApplicationStateActive) {
//1.創(chuàng)建本地通知的對(duì)象
UILocalNotification*notification = [[UILocalNotificationalloc]init];
//2.設(shè)置通知觸發(fā)的時(shí)間點(diǎn)
notification.fireDate= [NSDatedate];
//3.設(shè)置彈出提示的內(nèi)容
notification.alertBody= [[userInfoobjectForKey:@"aps"]objectForKey:@"alert"];
notification.userInfo= userInfo;
//4.提示按鈕的標(biāo)題
notification.alertAction= [userInfoobjectForKey:@"關(guān)閉"];
notification.soundName=UILocalNotificationDefaultSoundName;
//注冊(cè)本地通知沥寥,注冊(cè)之后,通知才會(huì)生效
[[UIApplicationsharedApplication]scheduleLocalNotification:notification];
//設(shè)置應(yīng)用程序圖標(biāo)上的數(shù)值
//[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.創(chuàng)建本地通知的對(duì)象
UILocalNotification*notification = [[UILocalNotificationalloc]init];
//2.設(shè)置通知觸發(fā)的時(shí)間點(diǎn)
notification.fireDate= [NSDatedate];
//3.設(shè)置彈出提示的內(nèi)容
notification.alertBody= [[userInfoobjectForKey:@"aps"]objectForKey:@"alert"];
notification.userInfo= userInfo;
//4.提示按鈕的標(biāo)題
notification.alertAction= [userInfoobjectForKey:@"關(guān)閉"];
notification.soundName=UILocalNotificationDefaultSoundName;
//注冊(cè)本地通知既忆,注冊(cè)之后驱负,通知才會(huì)生效
[[UIApplicationsharedApplication]scheduleLocalNotification:notification];
//設(shè)置應(yīng)用程序圖標(biāo)上的數(shù)值
//[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];
}
//獨(dú)立客戶端回調(diào)函數(shù)
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//
//[self parse:url application:application];
//return YES;
//}