iOS积累用之iOS地图iOS直播视频

iOS定向推送和订阅号推送

2016-11-18  本文已影响659人  小二同學

订阅号推送

类似于这样


BOSS小秘书.png

是这样一个需求,不经过后台,就是你收到一条消息,然后可以收到消息内容,并展示,然后还要显示历史消息,我刚开始接到这个需求的时候,也懵逼了。想了半天,选择用极光了,不过极光iOS不支持富媒体推送,就用的普通推送,上传的时候有点不完美,纯手动。

极光推送key value.png
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

不管APP在前台和在后台,收到通知点击消息,都会走这个方法。

application.applicationState == UIApplicationStateActive
//定义空数组
    self.PushArray = [NSMutableArray arrayWithCapacity:0];
    
    //先取
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    self.PushArray = [user mutableArrayValueForKey:@"PushArray"];
    [self.PushArray addObject:userInfo];
    
    
    if (self.PushArray.count!=0){
    NSUserDefaults * user1 = [NSUserDefaults standardUserDefaults];
    [user1 setObject:self.PushArray forKey:@"PushArray"];
    [user1 synchronize];
        
        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    }
    
    if (application.applicationState == UIApplicationStateActive) {
        NSLog(@"active");
        //程序当前正处于前台
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"您有一条新消息"
                                                            message:nil
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"点击查看",nil];
        [alertView show];
    }
    else if(application.applicationState == UIApplicationStateInactive)
    {
//        NSLog(@"inactive");
        [[NSNotificationCenter defaultCenter]postNotificationName:@"JPsh" object:nil];
        
    }
[self.tabelView setContentOffset:CGPointMake(0, self.dataArray.count*300*ScaleNumberHeight) animated:YES];

定向推送

就是当APP提交订单,立即就会收到一个推送消息。跨APP也能使用。

[self jPushwithTags:[NSString stringWithFormat:@"%d",user.userId]];
//极光推送设置标签tags 
- (void)jPushwithTags:(NSString *)Tags
{
    
    __autoreleasing NSMutableSet *tags = [NSMutableSet set];
    
    //重新赋值标签
    [[NSUserDefaults standardUserDefaults] setObject:Tags forKey:@"JPushTags"];
    
    //得到别名
    NSString * alias =  [[NSUserDefaults standardUserDefaults] objectForKey:@"JPushAlias"];
    
    
    [tags addObject:Tags];
    
    [JPUSHService setTags:tags alias:alias callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self];
    
}
#pragma  mark ------极光推送------
- (void)tagsAliasCallback:(int)iResCode
                     tags:(NSSet *)tags
                    alias:(NSString *)alias {
    NSString *callbackString =
    [NSString stringWithFormat:@"%d, \ntags: %@, \nalias: %@\n", iResCode,
     [self logSet:tags], alias];
    NSLog(@"TagsAlias回调:%@", callbackString);
}

- (NSString *)logSet:(NSSet *)dic {
    if (![dic count]) {
        return nil;
    }
    NSString *tempStr1 =
    [[dic description] stringByReplacingOccurrencesOfString:@"\\u"
                                                 withString:@"\\U"];
    NSString *tempStr2 =
    [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    NSString *tempStr3 =
    [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str =
    [NSPropertyListSerialization propertyListFromData:tempData
                                     mutabilityOption:NSPropertyListImmutable
                                               format:NULL
                                     errorDescription:NULL];
    return str;
}


- (void)setTags:(NSMutableSet **)tags addTag:(NSString *)tag {
    //  if ([tag isEqualToString:@""]) {
    // }
    [*tags addObject:tag];
}
极光定向推送.png

结语

有木有很简单。另外大家有什么关于极光推送的问题可以一起探讨哦,最近一直在做推送,趁热能想起来。信不信,不出一周,这些代码都是别人写的了。

上一篇下一篇

猜你喜欢

热点阅读