点击推送跳转到相应页面的注意事项:https://blog.csdn.net/yuqingzhude/article/details/78559810
ios10极光推送:
//**************************************极光推送
//初始化apns
//Required
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 10.0) {
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntityalloc] init];
entity.types =UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];
}
elseif ([[UIDevicecurrentDevice].systemVersionfloatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}
else {
//categories 必须为nil
[JPUSHServiceregisterForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
//初始化jpush
//如不需要使用IDFA,advertisingIdentifier可为nil
[JPUSHServicesetupWithOption:launchOptionsappKey:@"cd926fe0e13afefc19bf59d8"
channel:@"channel"
apsForProduction:false
advertisingIdentifier:nil];
/******极光推送*****/
//***************极光推送
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"注册成功");
/// Required - 注册 DeviceToken
[JPUSHServiceregisterDeviceToken:deviceToken];
}
// iOS 10 Support----app在前台收到推送的时候调用;
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
[JPUSHServicehandleRemoteNotification:userInfo];
}
NSLog(@"极光推送板");
completionHandler(UNNotificationPresentationOptionAlert);// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// iOS 10 Support----点击通知栏横幅的时候调用;
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request;// 收到推送的请求
UNNotificationContent *content = request.content;// 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
[JPUSHServicehandleRemoteNotification:userInfo];//处理接收到的通知
NSLog(@"iOS10收到远程通知");
}
else {
// 判断为本地通知
NSLog(@"iOS10收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(); // 系统要求执行这个方法
}
====================================
#import "AppDelegate.h"
#import "JPUSHService.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//d03295c5a9ace38da6f660b0
[JPUSHServiceregisterForRemoteNotificationTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
[JPUSHServicesetupWithOption:launchOptionsappKey:@"d03295c5a9ace38da6f660b0"channel:@"App Store"apsForProduction:NO];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"%@",deviceToken);
// Required
[JPUSHService registerDeviceToken:deviceToken];
[JPUSHServicesetAlias:@"5689"callbackSelector:nilobject:nil];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// IOS 7 Support Required
[JPUSHServicehandleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
=============项目中的======
接收消息推送的几种情况:
app在前台:有推送消息时,走willPresentNotification:这个方法;不会有横幅;app会有角标;app直接杀死,但是角标数字还在;
app关闭状态:收到推送消息时,会有横幅出现,app角标会增加;点击APP或点击横幅会启动app,先走didFinishLaunchingWithOptions这个方法;然后才会走接收通知的方法didReceiveNotificationResponse;
APP在后台:收到推送消息,会有横幅,APP角标会增加;点击横幅或者点击app会走applicationWillEnterForeground:这个方法,在这个方法里面设置tabbar右上角的小红点;点击横幅会走接收通知的方法didReceiveNotificationResponse;
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
@interface AppDelegate ()<JPUSHRegisterDelegate>
@end
@implementation AppDelegate
//极光推送
-(void)JPushSet{
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
NSLog(@"--jiguangatoken-001-");
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
[JPUSHService resetBadge];//清空JPush服务器中存储的badge值
}
//
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"--jiguangatoken---%@",deviceToken);
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
//注册失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
//在前台时有消息推送时调用;
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
[[NSNotificationCenter defaultCenter]postNotificationName:@"tuisongxiaoxiHongdian" object:nil];//推送消息小红点
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
[[NSNotificationCenter defaultCenter]postNotificationName:@"apnsInfo" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter]postNotificationName:@"tuisongxiaoxiHongdian" object:nil];//推送消息小红点
// UNNotificationRequest *request = notification.request;//收到推送的请求
// UNNotificationContent *content = request.content;//收到推送的消息内容
// NSNumber *badge = content.badge; //推送消息的角标
// NSString *body = content.body; //推送消息体
// UNNotificationSound *sound = content.sound; //推送消息的声音
// NSString *subtitle = content.subtitle; //推送消息的副标题
// NSString *title = content.title; //推送消息的标题
//
// NSLog(@"useINfo---%@--%@--%@--%@---%@",body,badge,sound,request,userInfo);
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//这是远程通知--前台收到的远程通知并显示
[JPUSHService handleRemoteNotification:userInfo];
}
NSLog(@"--jiguangatoken-002-");
completionHandler(UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// iOS 10 Support---点击通知栏横幅调用
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
[[NSNotificationCenter defaultCenter]postNotificationName:@"tuisongxiaoxiHongdian" object:nil];//推送消息小红点
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
NSLog(@"--推送消息---%@",userInfo);
[[NSNotificationCenter defaultCenter]postNotificationName:@"apnsInfo" object:nil userInfo:userInfo];
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//则是远程通知---处理远程通知并显示
[JPUSHService handleRemoteNotification:userInfo];
}
NSLog(@"--jiguangatoken-003-");
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
[JPUSHService resetBadge];//清空JPush服务器中存储的badge值
completionHandler(UIBackgroundFetchResultNewData);
//这里可以根据收到的推送内容来跳转到指定的地方;
}
//----------极光推送自定义消息
-(void)autoDefinemessage{
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
}
// 自定义消息接收到通知事件
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary *userInfo=[notification userInfo];
NSLog(@"---userinfo---%@",userInfo);
[LYCommanManager sharedManager].jpushAutodefineMessageDict=userInfo;
// NSString *content = [userInfo valueForKey:@"content"];
// NSDictionary *extras = [userInfo valueForKey:@"extras"];
// NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//=====================这里是APP是关闭的状态,有推送消息,点击app时的设置;
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){//如果APP的角标大于0,就存一个到哪里标记有新消息,以便在APP内部tabbarItem的右上角标记红点
[LYCommanManager sharedManager].istuisong=@"1";
}else {
[LYCommanManager sharedManager].istuisong=@"0";
}
[self JPushSet];//极光推送注册
[JPUSHService setupWithOption:launchOptions appKey:@"a6a3ffecd1cd036d8d9b9e0c"
channel:@"Publish channel"
apsForProduction:1
advertisingIdentifier:nil];
[self autoDefinemessage];//极光推送自定义消息---需要后台自己推,不去经过apn服务器
//设置根视图控制器-----上面保存角标标志要在这个之前
[self setupRootViewController];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"即将变得活跃");
}
//程序进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"进入后台");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"即将进入前台");
//接收到消息推送,app从后台进入前台的设置
LYMainTabBarController *mt=(LYMainTabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
NSLog(@"---在APP的类gate中获取tabbarController---%@",mt.viewControllers[2].tabBarItem.title);
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
[LYCommanManager sharedManager].istuisong=@"1";
[mt.viewControllers[2].tabBarItem setBadgeValue:@"1"];
[mt.viewControllers[2].tabBarItem setBadgeColor:[UIColor redColor]];
}else {
[LYCommanManager sharedManager].istuisong=@"0";
}
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
[JPUSHService resetBadge];//清空JPush服务器中存储的badge值
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"程序变得活跃");
}
- (void)applicationWillTerminate:(UIApplication *)application {
// //将要退出程序
}
//用于静默推送,当静默推送的时候调用此方法从iOS7开始有的,iOS10 任然有效
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"用于静默推送");
completionHandler(UIBackgroundFetchResultNewData);
}
@end
--极光推送自定义消息
-(void)autoDefinemessage{
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkDidReceiveMessage:)
name:kJPFNetworkDidReceiveMessageNotification
object:nil];
}
// 自定义消息接收到通知事件
- (void)networkDidReceiveMessage:(NSNotification *)notification {
NSDictionary *userInfo=[notification userInfo];
NSLog(@"---userinfo---%@",userInfo);
[LYCommanManager sharedManager].jpushAutodefineMessageDict=userInfo;
// NSString *content = [userInfo valueForKey:@"content"];
// NSDictionary *extras = [userInfo valueForKey:@"extras"];
// NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服务端传递的Extras附加字
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//=====================这里是APP是关闭的状态,有推送消息,点击app时的设置;
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){//如果APP的角标大于0,就存一个到哪里标记有新消息,以便在APP内部tabbarItem的右上角标记红点
[LYCommanManager sharedManager].istuisong=@"1";
}else {
[LYCommanManager sharedManager].istuisong=@"0";
}
[self JPushSet];//极光推送注册
[JPUSHService setupWithOption:launchOptions appKey:@"a6a3ffecd1cd036d8d9b9e0c"
channel:@"Publish channel"
apsForProduction:1
advertisingIdentifier:nil];
[self autoDefinemessage];//极光推送自定义消息---需要后台自己推,不去经过apn服务器
//设置根视图控制器-----上面保存角标标志要在这个之前
[self setupRootViewController];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"即将变得活跃");
}
//程序进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"进入后台");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"即将进入前台");
//接收到消息推送,app从后台进入前台的设置
LYMainTabBarController *mt=(LYMainTabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
NSLog(@"---在APP的类gate中获取tabbarController---%@",mt.viewControllers[2].tabBarItem.title);
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
[LYCommanManager sharedManager].istuisong=@"1";
[mt.viewControllers[2].tabBarItem setBadgeValue:@"1"];
[mt.viewControllers[2].tabBarItem setBadgeColor:[UIColor redColor]];
}else {
[LYCommanManager sharedManager].istuisong=@"0";
}
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
[JPUSHService resetBadge];//清空JPush服务器中存储的badge值
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"程序变得活跃");
}
- (void)applicationWillTerminate:(UIApplication *)application {
// //将要退出程序
}
//用于静默推送,当静默推送的时候调用此方法从iOS7开始有的,iOS10 任然有效
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"用于静默推送");
completionHandler(UIBackgroundFetchResultNewData);
}
@end