推送的設置html
/*程序員
launchOptions 爲NSDictionary類型的對象,裏面存儲有此程序啓動的緣由數組
1.若用戶直接啓動,lauchOptions內無數據;app
2.若由其餘應用程序經過openURL:啓動,則UIApplicationLaunchOptionsURLKey對應的對象爲啓動URL(NSURL,UIApplicationLaunchOptionsSourceApplicationKey對應啓動的源應用程序的bundle ID (NSString);ide
3.若由本地通知啓動,則UIApplicationLaunchOptionsLocalNotificationKey對應的是爲啓動應用程序的的本地通知對象(UILocalNotification);ui
4.若由遠程通知啓動,則UIApplicationLaunchOptionsRemoteNotificationKey對應的是啓動應用程序的的遠程通知信息userInfo(NSDictionary);url
其餘key還有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,spa
UIApplicationLaunchOptionsNewsstandDownloadsKey。orm
若要使用遠程推送,知足兩個條件:htm
1、用戶需 要調用註冊用戶推送registerUserNotificationSettings;
2、在info.plist文件中UIBackgroundModes必須包含遠程通知。
當App進入到後臺時,能夠有一段時間作處理工做。對於長時間運行的任務,須要在Info.plist添加一行,鍵爲UIBackgroundModes,值爲一個數組
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
_pushArray = [[NSMutableArray alloc] init]; //接受遠程推送消息的列表
if (launchOptions != nil)
{
DLog(@"LUN:%@", launchOptions);
// UIApplicationLaunchOptionsRemoteNotificationKey 這個key值就是push的信息
NSDictionary *dic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
DLog(@"LUN:%@", dic);
// 爲了複用代碼,統一到下面這個處理方法中handlePushNotify.
if([dic objectForKey:@"pushType"] != nil)
{
if ([[dic objectForKey:@"pushType"]intValue] ==1 || [[dic objectForKey:@"pushType"]intValue] == 2)
{
[WCAlertView showAlertWithTitle:@"標題"
message:[[dic objectForKey:@"aps"] objectForKey:@"alert"]
customizationBlock:^(WCAlertView *alertView) {
} completionBlock:^(NSUInteger buttonIndex, WCAlertView *alertView) {
} cancelButtonTitle:@"肯定" otherButtonTitles: nil];
}else
{
[[YFGlobalVariables sharedInstance].pushMesArray addObject:dic];
[_pushArray addObject:dic];
}
}
}
/*
iOS8擁有了全新的通知中心,有全新的通知機制
當屏幕頂部收到推送時只須要往下拉,就能看到快速操做界面,並不須要進入該應用才能操做。在鎖屏界面,對於推送項目也能夠快速處理。
基本上就是讓用戶儘可能在不離開當前頁面的前提下處理推送信息,再次提升處理效率。
可以進行直接互動的短信、郵件、日曆、提醒,第三方應用,可讓你不用進入程序就能進行快捷操做,並專一於手中正在作的事情。
*/
/*
咱們須要注意這個UIUserNotificationActionContextDefault,若是咱們使用這個,咱們會獲得這個推送行爲,Maybe和Accept
咱們還可使用UIUserNotificationActionContextMinimal獲得的是Decline和Accept行爲
*/
if ([getSystemVersion() floatValue] >= 8.0) {
//這裏主要是針對iOS 8.0,相應的8.1,8.2等版本各程序員可自行發揮,若是蘋果之後推出更高版本還不會使用這個註冊方式就不得而知了……
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"action";//按鈕的標示
action.title=@"Accept";//按鈕的標題
action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啓動程序
// action.authenticationRequired = YES;
// action.destructive = YES;
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"Reject";
action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啓動程序,在後臺處理
action.authenticationRequired = YES;//須要解鎖才能處理,若是action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;
action.destructive = YES;
//2.建立動做(按鈕)的類別集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";//這組動做的惟一標示,推送通知的時候也是根據這個來區分
[categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
//3.建立UIUserNotificationSettings,並設置消息的顯示類類型
/*+ (instancetype)settingsForTypes:(UIUserNotificationType)types categories:(NSSet *)categories;
* 1.參數:推送類型
* 2.參數:推送 categories 集合
*/
UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeAlert |
UIRemoteNotificationTypeSound)
categories:[NSSet setWithObjects:categorys, nil]];
[application registerUserNotificationSettings:notiSettings];
}else
{
UIRemoteNotificationType types =(UIRemoteNotificationTypeBadge
|UIRemoteNotificationTypeSound
|UIRemoteNotificationTypeAlert);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
}
//消息推送支持的類型,註冊消息推送
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([sourceApplication isEqualToString:@"com.meiYa.shineTour"])
{
return YES;
}
else
return NO;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url)
{
return NO;
}
return YES;
}
#pragma mark --處理推送
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//成功註冊registerUserNotificationSettings:後,回調的方法 iOS8
[application registerForRemoteNotifications];
}
//這裏輸出的DeviceToken爲尖括號括起來的一串數字,是NSData類型。
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
DLog(@"獲取DeviceToken成功: {%@}",deviceToken);
NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
#if 1
[YFGlobalVariables sharedInstance].deviceID = [[token substringWithRange:NSMakeRange(1, token.length-2)]
stringByReplacingOccurrencesOfString:@" "
withString:@""];
#elif 0
[YFGlobalVariables sharedInstance].deviceID = [[[[token description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] ;
#endif
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
DLog(@"註冊消息推送失敗,Register Remote Notifications error:{%@}",[error localizedDescription]);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// 處理推送消息
DLog(@"處理收到的遠程消息推送,userinfo:%@",userInfo);
DLog(@"收到推送消息:%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
if ([[userInfo objectForKey:@"pushType"]intValue] ==1 || [[userInfo objectForKey:@"pushType"]intValue] == 2)
{
[[YFGlobalVariables sharedInstance].pushMesArray addObject:userInfo];
[_pushArray addObject:userInfo];
[WCAlertView showAlertWithTitle:@"標題"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
customizationBlock:^(WCAlertView *alertView) {
} completionBlock:^(NSUInteger buttonIndex, WCAlertView *alertView) {
if (buttonIndex == 1) {
[self checkNotificationMessage];
}
else
{
[self.pushArray removeLastObject];
}
} cancelButtonTitle:@"取消" otherButtonTitles:@"查看", nil];
}
else
{
[WCAlertView showAlertWithTitle:@"標題"
message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
customizationBlock:^(WCAlertView *alertView) {
} completionBlock:^(NSUInteger buttonIndex, WCAlertView *alertView) {
} cancelButtonTitle:@"肯定" otherButtonTitles: nil];
}
}
#pragma mark - 推送消息的規則判斷
// 根據 信息 跳轉到 對應的界面 中 YFIndexViewController爲首頁的界面 視圖控制器
/*
*/
- (void)checkNotificationMessage
{
YFIndexViewController *viewControllers = [[self.tabbarController viewControllers] objectAtIndex:0];
[viewControllers pushNotficationViewController];
}
//YFIndexViewController中的方法 pushNotficationViewController
//#define kAppDelegate ((YFAppDelegate *)[UIApplication sharedApplication].delegate)
-(void)pushNotficationViewController
{
if (kAppDelegate.pushArray.count > 0) {
if ([NSString emptyOrNull:[YFGlobalVariables sharedInstance].userInfoModel.userID]) {
[self showLoginView];
}
else
{
[self pushMessageCheck];
}
}
}
- (void)pushMessageCheck
{
if (kAppDelegate.pushArray.count > 0) {
NSMutableDictionary *userDictionary = [YFFileManager readUserLoginDefaultWithName:@"userLogin"];
NSDictionary *dic = [kAppDelegate.pushArray lastObject];
NSString *userName =[dic objectForKey:@"userName"];
if ([userName isEqualToString:[userDictionary objectForKey:@"userName"]]) {
if ([[[getAppDelegate().pushArray lastObject] objectForKey:@"pushType"]intValue]==2)
{
[YFGlobalVariables sharedInstance].isLowPricePush = YES;
[self getCurrentUserInfo];
}else
{
if (getAppDelegate().pushArray.count != 0)
{
NSDictionary *dictionary =[[NSDictionary alloc] initWithDictionary:[getAppDelegate().pushArray lastObject]];
[self lookForOrderDetail:[dictionary objectForKey:@"orderNo"] orderType:[[dictionary objectForKey:@"orderType"] integerValue]];
}
}
}
else
{
[kAppDelegate.tabbarController selectTabAtItem:YFTabBarIndexItem];
[YFUtil accoutLogout];
NSMutableDictionary *loginDic = [[NSMutableDictionary alloc] init];
[loginDic setObject:userName forKey:@"userName"];
[loginDic setObject:@"" forKey:@"userPassword"];
[loginDic setObject:[NSNumber numberWithBool:NO] forKey:@"isAutoLogin"];
[YFFileManager saveUserLoginDefaultsDataWithName:@"userLogin" saveObject:loginDic];
[self showLoginView];
}
}
}
//審批推送時的跳轉
-(void)lookForOrderDetail:(NSString*)orderString orderType:(NSInteger)orderType;
{
//[self showLoadWaitView];
if (orderType == 1) {
YFAirOrderDetailViewController *airOrderController = [[YFAirOrderDetailViewController alloc] initWithOrderNo:orderString];
[self.navigationController pushViewController:airOrderController animated:YES];
}
else if(orderType == 2)
{
YFChangeOrderDetailViewController *changeDetailController = [[YFChangeOrderDetailViewController alloc] initWithOrderInfo:orderString];
[self.navigationController pushViewController:changeDetailController animated:YES];
}
else if(orderType == 3)
{
YFReturnOrderDetailViewController *returnOrderController = [[YFReturnOrderDetailViewController alloc] initWithOrderInfo:orderString];
[self.navigationController pushViewController:returnOrderController animated:YES];
}
else if(orderType == 5)
{
YFHotelOrderDetailViewController *hotelViewController = [[YFHotelOrderDetailViewController alloc] init];
hotelViewController.orderNo = orderString;
[self.navigationController pushViewController:hotelViewController animated:YES];
}
else if(orderType == 70)
{
YFTrainOrderDetailViewController *orderDetailController = [[YFTrainOrderDetailViewController alloc] init];
orderDetailController.orderNo = orderString;
[self.navigationController pushViewController:orderDetailController animated:YES];
}else if(orderType == 80)
{
YFTrainReturnTicketViewController *returnOrderDetailController = [[YFTrainReturnTicketViewController alloc] init];
returnOrderDetailController.orderNo = orderString;
[self.navigationController pushViewController:returnOrderDetailController animated:YES];
}
}
最後 :手把手教你 iOS推送
http://www.cocoachina.com/industry/20130321/5862.html