项目:PushDemo
APP应用场景:集成极光推送、百度推送、爱心推推送SDK,接受自己服务器后台的推送。
功能:
1、自定义存储log日志,存储到沙盒,文件格式为txt,每天记录一个文件,DDLog也同时记录,可在iTunes文件共享中查看txt和log文件,txt文件可以使用苹果自带的分享功能分享至其他设备(airdrop)或APP(微信、QQ等)。
主要代码:
//写日志
-(void)writeLogWithTxt:(NSString *)string{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
//获取沙盒路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *dateAndTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"pushDemo_%@.txt",dateAndTime];
//目录路径
NSString *dirPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PushLogs"];
//获取文件路径
NSString *theFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"PushLogs/%@",fileName]];
//创建文件管理器
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir = NO;
BOOL existed = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
//如果文件夹不存在就创建
if (!(isDir && existed)) {
[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
//如果文件不存在 创建文件
if(![fileManager fileExistsAtPath:theFilePath]){
NSString *str = @"日志开始记录 \n\n";
[str writeToFile:theFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
NSLog(@"所写内容=%@",string);
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:theFilePath];
[fileHandle seekToEndOfFile]; //将节点跳到文件的末尾
NSData* stringData = [[NSString stringWithFormat:@"%@\n",string] dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:stringData]; //追加写入数据
[fileHandle closeFile];
}
});
}
// 自定义log存入 txt文件 每天存一版 分享时分享当天txt
//想要查看全的log 设备连接iTunes 点击设备 文件共享 点击当前APP 右侧文稿目录下 logs目录下为正宗log日志,PushLogs目录下为自定义的txt 日志
#define MyLogWrite(string) \
({\
DDLogInfo(string);NSString * founStr = [NSString stringWithFormat:@"%@:%@",[self class],NSStringFromSelector(_cmd)];NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;dateFormatter.dateFormat = @"yyyy-MM-dd HH:MM:SS";NSString *dateAndTime = [dateFormatter stringFromDate:[NSDate date]]; NSString *formatterStr = [NSString stringWithFormat:@"%@ %@ | %@",dateAndTime,founStr,string];[[MyLogManger sharedLogManger] writeLogWithTxt:formatterStr];\
})
//复制日志 分享到其他APP txt文件格式
- (IBAction)copyTheLogClick:(UIButton *)sender {
[self.view endEditing:YES];
MyLogWrite(@"User did click copy log Button");
//获取沙盒路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
//获取文件路径
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.formatterBehavior = NSDateFormatterBehavior10_4;
dateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *dateAndTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"pushDemo_%@.txt",dateAndTime];
NSString *theFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"PushLogs/%@",fileName]];
NSURL *URL = [NSURL fileURLWithPath:theFilePath];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[URL] applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
activityViewController.popoverPresentationController.sourceRect = ((UIButton *)sender).frame;
[self presentViewController:activityViewController animated:YES completion:NULL];
}
2、添加和删除各自推送的标签,查看标签列表
3、获取推送ID,显示推送过来的消息。
//收到推送消息
-(void)hasReceivedPushMessage:(NSNotification *)notification{
//转为json 记录和显示
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notification.object options:NSJSONWritingPrettyPrinted error:&error];
if (!error) {
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];;
_textView.text = jsonStr;
MyLogWrite(([NSString stringWithFormat:@"have received push message : %@",jsonStr]));
//保存数据
NSMutableArray *dataArr = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PushMessageList"] mutableCopy];
if (!dataArr) {
dataArr = [[NSMutableArray alloc] init];
}
if (![dataArr containsObject:jsonStr]) {
[dataArr insertObject:jsonStr atIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:dataArr forKey:@"PushMessageList"];
}
}else{
MyLogWrite(([NSString stringWithFormat:@"have received push message but change to json failed error: %@",[error localizedDescription]]));
_textView.text = [NSString stringWithFormat:@"数据解析json失败,error:\n%@",[error localizedDescription]];
}
}
4、将消息存储到本地,在消息列表中显示出来,还可以是清除消息列表。
总结:
1、极光和百度推送有自己的推送ID 单推时使用各自的ID即可,爱心推使用的是苹果注册的deviceToken。
2、爱心推设置标签格式要求必须是数字类型,为16位,二进制的16位及2^15 - 1 ,小于等于32767。其他推送标签正常。
3、各自的SDK版本号信息都无法根据SDK版本变更自动变化,只能手动修改
//SDK信息
//JPUSH_VERSION_NUMBER 版本号 类型无法直接显示
NSString *JPushSdkInfo = [NSString stringWithFormat:@"极光推送SDK当前版本:%@",JPushSDKVersion];
//BPush.h最上方注释
NSString *BPushSdkInfo = [NSString stringWithFormat:@"百度推送SDK当前版本:%@",BPushSDKVersion];
//官网下载SDK时包名
NSString *IXiPushSdkInfo = [NSString stringWithFormat:@"爱心推SDK当前版本:%@",IXPushSDKVersion];