//// Created by 徐岸 on 2017/5/19.// Copyright © 2017年 xuan. All rights reserved.//#import "AppDelegate.h"// 引 JPush功能所需头 件#import "JPUSHService.h"// 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) {// 判断是否通知进来 //值得注意的是程序在杀死的情况下就会走当前 didFinishLaunching .. 的方法
//第三中情况就是程序在杀死的情况下,因为程序在杀死的时候你根本就不能接收到通知,所以不能通过通知去做标记,,我在这里采用的是在点击接收到远程通知的情况下在偏好设置里面保存一个值,然后在进入到你的要跳转之前的界面去,判断是否保存了这个值,保存了这个值就代表你是点击远程消息进来的,,在做跳转
NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotification) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"YES" forKey:@"Notification"];
[defaults synchronize];
}
}
//Required
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
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新增的注册 法,改成可上报IDFA,如果没有使 IDFA直接传nil
// 如需继续使 pushConfig.plist 件声明appKey等配置内容,请依旧使
[JPUSHService setupWithOption:launchOptions appKey:@"你在极光创建应用的appKey"
channel:@"App Store"
apsForProduction:YES // YES代表是生成状态, NO是发布状态(打包前注意检查应用环境)
advertisingIdentifier:nil];
return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
// 发送数据给极光 极光帮助程序员保存数据
// 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];
}
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
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(); // 系统要求执行这个方法
}
// 远程推送通知,点击消息会调用的方法
- (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];
// 第一种情况前台运行 (我的处理是弹出一个弹框在前台做的提示处理,首先的是你要拿到当前在屏幕显示的控制器);
NSString *apnCount = userInfo[@"aps"][@"alert"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示您有一条推送信息" message:apnCount delegate:self cancelButtonTitle:@"查看" otherButtonTitles:@"取消", nil];
alert.delegate = self;
[alert show];
}else{
//第二种情况后台挂起时
[self goToMssageViewControllerWithDict:nil];
}
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[application setApplicationIconBadgeNumber:0]; //清除角标
[application cancelAllLocalNotifications];
}
//远程通知跳转
-(void)goToMssageViewControllerWithDict:(NSDictionary *)dict
{
//此处的处理方法是点击消息跳转界面到指定要跳转之前的控制器1.然后发送一条通知,2在跳转界面到指定要跳转之前的控制器你去接收通知,,如果接收到通知了就代表是点击远程消息进来的,最后进行跳转
UITabBarController *tabBarC = (UITabBarController *)self.window.rootViewController;
// 跳转控制器
tabBarC.selectedIndex = 0;
[[NSNotificationCenter defaultCenter] postNotificationName:XAPushNotification object:nil];
}
/**
注意点 1.Xcode 8.0要进行开启远程通知的权限,在app 设置里 ->capabilities开启
2.如果你集成的极光推送要与你们的后台服务器进行对接,那你就要配置一个命名叫PushConfig的plist文件.文件的作用,好像就是告诉后台一写信息,里面有三个的key,不知道怎么配就百度下哦.
3.苹果远程推送如果你的应用正在前台,是接收不到远程提示的,但是你能接收到通知的哦,,qq,微信哪种是自定义的处理哦.. 你可以参考我上面的处理,在做操作
4.这里的功能代码适合的程度是:1.实现app在三种转台下都能接收到消息的处理,2.实现可自己这边的后台服务器与机关服务器的对接功能,可以直接通过自己这边的服务器去发送远程通知,3.接收到消息能跳转到单个指定的控制器,
*/
/**
常见问题点击推送消息发送成功但收不到消息>以及代码里显示成功但极光上成功发送,接收为0的建议:
1.确保你的appKey,证书,推送权限是否开启,上线环境是否匹配.
2.如果你们后台对接了极光服务器,你是否配置了PushConfig的plist服务器.没有配置是接收不到的
3.检查代码,判断排除,你的代码接收到了极光发送过来的通知没
4.确保是否是因为极光服务器的延迟导致通知阻塞,或者是极光服务器炸了,这我真遇到了...
5.一定要加入一下极光群,在官网有哦,关注下最新的动态,进行交流提问,你面有大神哦.
*/
@end