极光推送全程监控

作者: _Comma | 来源:发表于2016-12-15 20:58 被阅读128次

小编其实也是个小白,第一次弄推送,分享给需要的朋友看看

[极光推送后台登录页面]
(https://www.jiguang.cn/accounts/login/form):

首先先去注册一个账户,创建应用然后上传对应证书极光推送证书指南,大家记住生产证书和开发证书不要弄错了 书友推送证书指南

配置就不多说了,下面直接代码,写的不对的大家多多给意见,小编表示很需要成长

38559DB8-E718-4D38-AC7F-216274B034D6.png
下面那个打码的是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];
    }
}

正在成长的小白,希望得得到大家的批评,哈哈

相关文章

  • 极光推送全程监控

    小编其实也是个小白,第一次弄推送,分享给需要的朋友看看 [极光推送后台登录页面](https://www.jigu...

  • 极光推送

    极光推送视频地址,非常详细的极光推送视频 极光推送

  • 极光推送

    极光推送 tagprivate void initJpush() {//TODO 极光推送// JPushInte...

  • 极光推送进行远程推送

    借阅:极光推送进行远程推送 怎么使用极光推送进行远程推送 在极光官网注册极光推送创建一个应用在应用配置中导入两个证...

  • ios极光推送

    第一次使用极光推送,在这里把极光推送的步骤说一下,省的以后再次用到极光推送的时候,给忘了,其实,极光推送不难...

  • 【知识总结】(2)远程推送

    推送SDK:极光推送 后台点击推送: iOS 10 以下收到推送点击触发 iOS 10 以上触发: 极光推送中使用...

  • 2018年功能模块沉淀

    一、推送模块 1.极光推送 文档:https://www.jiguang.cn/push备注:极光推送包括普通推送...

  • 极光推送(二)——推送的使用

    前言 在极光推送(一)——配置中讲过了极光推送的配置,这节讲讲极光推送的使用参考文档极光官网 下面以我写的demo...

  • 极光推送集成开发

    1.极光推送集成与设置 极光推送地址①注册极光推送账号。②在应用管理内按照步骤创建APP。③找到“文档——iOS—...

  • Flutter开发 集成极光推送

    Flutter推送 极光推送Flutter版本 最近研究Flutter推送,在网上找了很多资料,发现极光推送竟然有...

网友评论

本文标题:极光推送全程监控

本文链接:https://www.haomeiwen.com/subject/sxvgmttx.html