码农的日常之iOS开发iOS开发极光

iOS开发—手把手教你最全、最新、代码最清晰、包含高级开发的极光

2016-08-30  本文已影响1402人  GavinKang

最近公司不忙,整理一下资料,以备以后直接用,本来计划全部整理的,但是发现这篇文章程胖出品——极光推送(手把手教你啊)对极光推送的配置工作,说的很详细了,现在我说一下接下来具体的全部推送、设备标签推送、设备别名推送

1——对于新手,在看程胖出品的极光推送,推送消息之前,建议还是要把监听加进去,防止出问题也不知道出在哪

//加在这个方法里面- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidSetup:)
                          name:kJPFNetworkDidSetupNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidClose:)
                          name:kJPFNetworkDidCloseNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidRegister:)
                          name:kJPFNetworkDidRegisterNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidLogin:)
                          name:kJPFNetworkDidLoginNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(networkDidReceiveMessage:)
                          name:kJPFNetworkDidReceiveMessageNotification
                        object:nil];
    [defaultCenter addObserver:self
                      selector:@selector(serviceError:)
                          name:kJPFServiceErrorNotification
                        object:nil];

对应的方法

- (void)networkDidSetup:(NSNotification *)notification {
    NSLog(@"已连接");
    
}

- (void)networkDidClose:(NSNotification *)notification {
    NSLog(@"未连接");
}

- (void)networkDidRegister:(NSNotification *)notification {
    NSLog(@"%@", [notification userInfo]);
    NSLog(@"已注册");
}

- (void)networkDidLogin:(NSNotification *)notification {
    
    NSLog(@"已登录");
    
    if ([JPUSHService registrationID]) {
        NSLog(@"get RegistrationID");
    }
}
- (void)serviceError:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    NSString *error = [userInfo valueForKey:@"error"];
    NSLog(@"-----%@", error);
}

- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    
    NSLog(@"d消息=====%@",userInfo);
    
}

2——对于每次推送的角标,可以用下面一句代码,在每次程序打开的时候去除,和上面的监听,写在同一个方法里

   [UIApplication sharedApplication].applicationIconBadgeNumber  = 0;

3——因为推送是默认程序在后台才进行推送,但是我们公司要求,在打开程序的时候,也可以接收到推送,在这种情况下,我们可以加上下面的方法


- (void)application:(UIApplication *)application didReceiveRemoteNotification
                   :(NSDictionary *)userInfo fetchCompletionHandler
                   :(void (^)(UIBackgroundFetchResult))completionHandler {
//    [HYBJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
    
    // IOS 7 Support Required
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    
    //下面是应用正在前台下以提示框的方式显示推送的代码
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"
                                                        message:userInfo[@"aps"][@"alert"]
                                                       delegate:nil
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定",nil];
        [alert show];
    }  
    return;  
}

4——前面这些已经满足一般公司的需求了,但是有些公司需要根据不同的设备标签和设备别名进行推送,具体文档可以点击进入官网查看,在设置标签和别名进行推送的地方,导入 #import "JPUSHService.h" 头文件,实现下面的代码就可以了

!!设备标签注意事项:
传 nil 表示此次调用不设置此值。
空集合([NSSet set])表示取消之前的设置。
集合成员类型要求为NSString类型
每次调用至少设置一个 tag,覆盖之前的设置,不是新增。
有效的标签组成:字母(区分大小写)、数字、下划线、汉字。
限制:每个 tag 命名长度限制为 40 字节,最多支持设置 1000 个 tag,但总长度不得超过7K字节。(判断长度需采用UTF-8编码)
单个设备最多支持设置 1000 个 tag。App 全局 tag 数量无限制。

!!设备别名注意事项:
传 nil 表示此次调用不设置此值。
空字符串 (@"")表示取消之前的设置。
每次调用设置有效的别名,覆盖之前的设置。
有效的别名组成:字母(区分大小写)、数字、下划线、汉字。
限制:alias 命名长度限制为 40 字节。(判断长度需采用UTF-8编码)

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   17-8-17更新
   __autoreleasing NSMutableSet *tags = [NSMutableSet set];
    //    设备标签  为安装了应用程序的用户,打上标签。其目的主要是方便开发者根据标签,来批量下发 Push 消息。可为每个用户打多个标签。 字母(区分大小写)、数字、下划线、汉字 每个 tag 命名长度限制为 40 字节
    [self setTags:&tags addTag:@"233"];
    [JPUSHService setTags:tags completion:^(NSInteger iResCode, NSSet *iTags, NSInteger seq) {
        NSLog(@"1111--iResCode---%ld----iTags--%@----seq--%ld",(long)iResCode,iTags,(long)seq);
    } seq:23];

 //    设备别名
    __autoreleasing NSString *alias = @"343434";
    
    [JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
       
        NSLog(@"1111--iResCode---%ld----iAlias--%@----seq--%ld",(long)iResCode,iAlias,(long)seq);

    } seq:45];

}

至此,设备标签和设备别名推送就已经完成了,你就可以选择推送方式进行推送了 选择推送方式

极光推送接收自定义消息:在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions 加入下面的代码:(具体的位置在极光推送 入门指南的 iOS SDK API中)

监听自定义消息 注册监听的代理

以前没有写过东西,现在感觉写东西的感觉挺好的,以后还会继续更新,希望大家关注,留言,互相交流

上一篇下一篇

猜你喜欢

热点阅读