您的位置:首页 > 移动开发 > Objective-C

[Object-C]使用个推遇到的坑

2015-07-31 17:26 896 查看

个推

一.简单介绍个推的功能

     在iOS 个推是不能够发送"推送通知"的,只能使用"透传消息".


描述: 就是关于这次透传信息的一个描述, 可以理解为表示, 没有什么实际的意义.
消息内容: 也就是最重要的我们需要的消息体.透传的消息内容.
Message: 这个我并没有收到过这样的信息.目前忽略不计, 但是需要填.

二. 关于代码集成.

// [1]:使用APPID/APPKEY/APPSECRENT创建个推实例
[self startSdkWith:kAppId appKey:kAppKey appSecret:kAppSecret];

// [2]:注册APNS
[self registerRemoteNotification];

// [2-EXT]: 获取启动时收到的APN数据
NSDictionary* message = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (message) {
NSString *payloadMsg = [message objectForKey:@"payload"];
NSString *record = [NSString stringWithFormat:@"[APN]%@, %@", [NSDate date], payloadMsg];
}

NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

- (void)GeTuiSdkDidReceivePayload:(NSString *)payloadId andTaskId:(NSString *)taskId andMessageId:(NSString *)aMsgId fromApplication:(NSString *)appId
{

// [4]: 收到个推消息

_payloadId = payloadId;

NSData* payload = [GeTuiSdk retrivePayloadById:payloadId];

NSString *payloadMsg = nil;
if (payload) {
payloadMsg = [[NSString alloc] initWithBytes:payload.bytes
length:payload.length
encoding:NSUTF8StringEncoding];
}
NSData *jsonData = [payloadMsg dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];

NSString *record = [NSString stringWithFormat:@"%d, %@, %@", ++_lastPaylodIndex, [self formateTime:[NSDate date]], payloadMsg];

NSLog(@"--+==+---%@", record);

// [4]: 收到个推消息
//     _payloadId = nil;
//    _payloadId = payloadId;
//    NSLog(@"%@----%@----%@---%@", payloadId,taskId, aMsgId, appId);
//    NSData* payload = [GeTuiSdk retrivePayloadById:payloadId];
//
//    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:payload options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
//localNotification.userInfo = userinfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
if (dic) {
localNotification.alertBody = [dic objectForKey:@"content"];
}else{
localNotification.alertBody = payloadMsg;
}
localNotification.fireDate = [NSDate date];
self.userInfo = [NSDictionary dictionaryWithDictionary:dic];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

三. 关于 上传项目遇到的坑.

1. app被拒.原因:

Your app declares support for audio in the UIBackgroundModes key in your Info.plist, but we were unable to play any audible
content when the application was running in the background.

Next Steps

The audio key is intended for use by applications that provide audible content to the user while in the background, such as music player or streaming
audio applications. Please revise your app to provide audible content to the user while the app is in the background or remove the "audio" setting from the UIBackgroundModes key.

2. 怎么解决吗?

原来是这样的.


但是为什么会出现这个呢? 原来发现个推会自动添加这个东西.....

3.把这个东西去掉之后, 好像是app在后台不能接收到推送了, 这个什么情况呢?

原来, 等到把这个去掉之后,app在后台的时候,个推就停止工作了.那怎么办? 好吧,去去掉一句话吧
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// [EXT] 切后台关闭SDK,让SDK第一时间断线,让个推先用APN推送
// [self stopSdk];

//[GeTuiSdk enterBackground]; // 这样个推在app在后台的时候也会工作.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  个推 iOS