小编其实也是个小白,第一次弄推送,分享给需要的朋友看看
[极光推送后台登录页面]
(https://www.jiguang.cn/accounts/login/form):
首先先去注册一个账户,创建应用然后上传对应证书极光推送证书指南,大家记住生产证书和开发证书不要弄错了 书友推送证书指南
配置就不多说了,下面直接代码,写的不对的大家多多给意见,小编表示很需要成长
下面那个打码的是appkey,JPUSHRegisterDelegate不要忘了添加这个代理哦
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
RootViewController *root = [RootViewController new];
_window.rootViewController = root;
[_window makeKeyAndVisible];
[self registJpush];
return YES;
}
- (void)registJpush {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
}
else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
}
else {
//categories 必须为nil
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)
categories:nil];
}
[JPUSHService setupWithOption:@{} appKey:JPushKey
channel:@"App Store"
apsForProduction:0 //上线改为1
advertisingIdentifier:@"7R974C6CJ6"];
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
[[NSUserDefaults standardUserDefaults] setValue:registrationID forKey:@"registID"];
}];
下面是代理方法
//注册APNS
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
// 应用在前台走的方法 我用来接收用户被挤下线的时候的通知,直接强行下线
- (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];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
//手机在后台/前台点击通知后台走的方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
// if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
// //如果应用在前台,在这里执行
// UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"极光推送"message:@"前台点击" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil,nil];
// [alertView show];
// }else if([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
//
//
// }
//我没有判断后台前台,点击都跳转界面,这个是跳转的通知,下面会写接收的代码
[[NSNotificationCenter defaultCenter] postNotificationName:@"backPush" object:nil userInfo:@{@"push":userInfo[@"push_driver"]}];
//参数是和后台约定好的
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系统要求执行这个方法
}
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
[application registerForRemoteNotifications];
}
清除角标什么的就不写了(其实我没怎么处理,等后面再优化)
我写一个我的界面跳转代码吧。我的RootViewController是TabbarController,所以接收到通知的方法就写到TabbarController 里面了(亲测前台后台都可以直接跳转),但是我的跳转是先跳到tabbar的某个下标,再push,大家可以根据自己需求怎么跳转,而且我这个感觉好麻烦 ><
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backPush:) name:@"backPush" object:nil];
- (void)backPush:(NSNotification *)noti {
NSString *string = noti.userInfo[@"push"];
if ([string isEqualToString:@"deposit"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"deposit"];
self.selectedIndex = 2;
//定位tabbar下标为2
}
}
这是跳转的那个控制器里面的
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"deposit"]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"deposit"];
//对应界面
UIViewController *viewC = [[UIViewController alloc]init];
[self.navigationController pushViewController: viewC animated:YES];
}
}
正在成长的小白,希望得得到大家的批评,哈哈