////? Created by 徐岸 on 2017/5/19.//? Copyright ? 2017年 xuan. All rights reserved.//#import "AppDelegate.h"http:// 引 JPush功能所需頭 件#import "JPUSHService.h"http:// iOS10注冊APNs所需頭 件#define XAPressNewsTVCOne @"XAInformationVCOne"#define XAPushNotification @"XAPushNotification"#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import#endif@interface AppDelegate ()@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert)? ? categories:nil];
} else {
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)
#else
categories:nil];
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)
#endif
categories:nil];
}
[JPUSHService setupWithOption:launchOptions];
if (launchOptions) {// 判斷是否通知進(jìn)來 //值得注意的是程序在殺死的情況下就會走當(dāng)前? didFinishLaunching .. 的方法
//第三中情況就是程序在殺死的情況下,因?yàn)槌绦蛟跉⑺赖臅r(shí)候你根本就不能接收到通知,所以不能通過通知去做標(biāo)記,,我在這里采用的是在點(diǎn)擊接收到遠(yuǎn)程通知的情況下在偏好設(shè)置里面保存一個(gè)值,然后在進(jìn)入到你的要跳轉(zhuǎn)之前的界面去,判斷是否保存了這個(gè)值,保存了這個(gè)值就代表你是點(diǎn)擊遠(yuǎn)程消息進(jìn)來的,,在做跳轉(zhuǎn)
NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"YES" forKey:@"Notification"];
[defaults synchronize];
}
}
//Required
//notice: 3.0.0及以后版本注冊可以這樣寫,也可以繼續(xù)用之前的注冊方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
// Required
// init Push
// notice: 2.1.5版本的SDK新增的注冊 法粘茄,改成可上報(bào)IDFA,如果沒有使 IDFA直接傳nil
// 如需繼續(xù)使 pushConfig.plist 件聲明appKey等配置內(nèi)容活玲,請依舊使
[JPUSHService setupWithOption:launchOptions appKey:@"你在極光創(chuàng)建應(yīng)用的appKey"
channel:@"App Store"
apsForProduction:YES? // YES代表是生成狀態(tài), NO是發(fā)布狀態(tài)(打包前注意檢查應(yīng)用環(huán)境)
advertisingIdentifier:nil];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注冊 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
// 發(fā)送數(shù)據(jù)給極光 極光幫助程序員保存數(shù)據(jù)
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
// 需要執(zhí)行這個(gè)方法螟左,選擇是否提醒用戶,有Badge、Sound匆骗、Alert三種類型可以選擇設(shè)置
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler();? // 系統(tǒng)要求執(zhí)行這個(gè)方法
}
// 遠(yuǎn)程推送通知,點(diǎn)擊消息會調(diào)用的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
//self.homeVC = (XAHomeController *)[self currentViewController];
// 第一種情況前臺運(yùn)行? (我的處理是彈出一個(gè)彈框在前臺做的提示處理,首先的是你要拿到當(dāng)前在屏幕顯示的控制器);
NSString *apnCount = userInfo[@"aps"][@"alert"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"溫馨提示您有一條推送信息" message:apnCount delegate:self cancelButtonTitle:@"查看" otherButtonTitles:@"取消", nil];
alert.delegate = self;
[alert show];
}else{
//第二種情況后臺掛起時(shí)
[self goToMssageViewControllerWithDict:nil];
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0];? //清除角標(biāo)
[application cancelAllLocalNotifications];
}
//遠(yuǎn)程通知跳轉(zhuǎn)
-(void)goToMssageViewControllerWithDict:(NSDictionary *)dict
{
//此處的處理方法是點(diǎn)擊消息跳轉(zhuǎn)界面到指定要跳轉(zhuǎn)之前的控制器1.然后發(fā)送一條通知,2在跳轉(zhuǎn)界面到指定要跳轉(zhuǎn)之前的控制器你去接收通知,,如果接收到通知了就代表是點(diǎn)擊遠(yuǎn)程消息進(jìn)來的,最后進(jìn)行跳轉(zhuǎn)
UITabBarController *tabBarC = (UITabBarController *)self.window.rootViewController;
// 跳轉(zhuǎn)控制器
tabBarC.selectedIndex = 0;
[[NSNotificationCenter defaultCenter] postNotificationName:XAPushNotification object:nil];
}
/**
注意點(diǎn)? 1.Xcode 8.0要進(jìn)行開啟遠(yuǎn)程通知的權(quán)限,在app 設(shè)置里 ->capabilities開啟
2.如果你集成的極光推送要與你們的后臺服務(wù)器進(jìn)行對接,那你就要配置一個(gè)命名叫PushConfig的plist文件.文件的作用,好像就是告訴后臺一寫信息,里面有三個(gè)的key,不知道怎么配就百度下哦.
3.蘋果遠(yuǎn)程推送如果你的應(yīng)用正在前臺,是接收不到遠(yuǎn)程提示的,但是你能接收到通知的哦,,qq,微信哪種是自定義的處理哦.. 你可以參考我上面的處理,在做操作
4.這里的功能代碼適合的程度是:1.實(shí)現(xiàn)app在三種轉(zhuǎn)臺下都能接收到消息的處理,2.實(shí)現(xiàn)可自己這邊的后臺服務(wù)器與機(jī)關(guān)服務(wù)器的對接功能,可以直接通過自己這邊的服務(wù)器去發(fā)送遠(yuǎn)程通知,3.接收到消息能跳轉(zhuǎn)到單個(gè)指定的控制器,
*/
/**
常見問題點(diǎn)擊推送消息發(fā)送成功但收不到消息>以及代碼里顯示成功但極光上成功發(fā)送,接收為0的建議:
1.確保你的appKey,證書,推送權(quán)限是否開啟,上線環(huán)境是否匹配.
2.如果你們后臺對接了極光服務(wù)器,你是否配置了PushConfig的plist服務(wù)器.沒有配置是接收不到的
3.檢查代碼,判斷排除,你的代碼接收到了極光發(fā)送過來的通知沒
4.確保是否是因?yàn)闃O光服務(wù)器的延遲導(dǎo)致通知阻塞,或者是極光服務(wù)器炸了,這我真遇到了...
5.一定要加入一下極光群,在官網(wǎng)有哦,關(guān)注下最新的動態(tài),進(jìn)行交流提問,你面有大神哦.
*/
@end