打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS 个推在获得推送消息的时候如何实现跳转到对应页面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(@"$$$$$$$$     %@",launchOptions);
// 创建keyWindow
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
loginViewController *login = [[loginViewController alloc]init];
[self.window setRootViewController:login];
// 设置为主窗口并且可见
[self.window makeKeyAndVisible];
// [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];
self.window.rootViewController = self.viewController;
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// [EXT] 切后台关闭SDK,让SDK第一时间断线,让个推先用APN推送
[self stopSdk];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// [EXT] 重新上线
[self startSdkWith:_appID appKey:_appKey appSecret:_appSecret];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
_deviceToken = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"deviceToken:%@", _deviceToken);
// [3]:向个推服务器注册deviceToken
if (_gexinPusher) {
[_gexinPusher registerDeviceToken:_deviceToken];
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
// [3-EXT]:如果APNS注册失败,通知个推服务器
if (_gexinPusher) {
[_gexinPusher registerDeviceToken:@""];
}
[_viewController logMsg:[NSString stringWithFormat:@"didFailToRegisterForRemoteNotificationsWithError:%@", [error localizedDescription]]];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userinfo
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
// [4-EXT]:处理APN
NSString *payloadMsg = [userinfo objectForKey:@"payload"];
NSString *record = [NSString stringWithFormat:@"[APN]%@, %@", [NSDate date], payloadMsg];
[_viewController logMsg:record];
self.window.rootViewController = self.viewController;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
// [4-EXT]:处理APN
NSString *payloadMsg = [userInfo objectForKey:@"payload"];
NSDictionary *aps = [userInfo objectForKey:@"aps"];
NSNumber *contentAvailable = aps == nil ? nil : [aps objectForKeyedSubscript:@"content-available"];
NSString *record = [NSString stringWithFormat:@"[APN]%@, %@, [content-available: %@]", [NSDate date], payloadMsg, contentAvailable];
[_viewController logMsg:record];
if ([UIApplication sharedApplication].applicationState ==UIApplicationStateActive ) {
orderInformationViewController *order = [[orderInformationViewController alloc]init];
[self.viewController presentViewController:order animated:YES completion:nil];
}
completionHandler(UIBackgroundFetchResultNewData);
}
- (void)startSdkWith:(NSString *)appID appKey:(NSString *)appKey appSecret:(NSString *)appSecret
{
if (!_gexinPusher) {
_sdkStatus = SdkStatusStoped;
self.appID = appID;
self.appKey = appKey;
self.appSecret = appSecret;
_clientId = nil;
NSError *err = nil;
_gexinPusher = [GexinSdk createSdkWithAppId:_appID
appKey:_appKey
appSecret:_appSecret
appVersion:@"0.0.0"
delegate:self
error:&err];
if (!_gexinPusher) {
[_viewController logMsg:[NSString stringWithFormat:@"%@", [err localizedDescription]]];
} else {
_sdkStatus = SdkStatusStarting;
}
[_viewController updateStatusView:self];
}
}
- (void)stopSdk
{
if (_gexinPusher) {
[_gexinPusher destroy];
_gexinPusher = nil;
_sdkStatus = SdkStatusStoped;
_clientId = nil;
[_viewController updateStatusView:self];
}
}
- (BOOL)checkSdkInstance
{
if (!_gexinPusher) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"SDK未启动" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
return NO;
}
return YES;
}
- (void)setDeviceToken:(NSString *)aToken
{
if (![self checkSdkInstance]) {
return;
}
[_gexinPusher registerDeviceToken:aToken];
}
- (BOOL)setTags:(NSArray *)aTags error:(NSError **)error
{
if (![self checkSdkInstance]) {
return NO;
}
return [_gexinPusher setTags:aTags];
}
- (NSString *)sendMessage:(NSData *)body error:(NSError **)error {
if (![self checkSdkInstance]) {
return nil;
}
return [_gexinPusher sendMessage:body error:error];
}
- (void)bindAlias:(NSString *)aAlias {
if (![self checkSdkInstance]) {
return;
}
return [_gexinPusher bindAlias:aAlias];
}
- (void)unbindAlias:(NSString *)aAlias {
if (![self checkSdkInstance]) {
return;
}
return [_gexinPusher unbindAlias:aAlias];
}
- (void)testGetClientId {
NSString *clientId = [_gexinPusher clientId];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"当前的CID" message:clientId delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alertView show];
}
#pragma mark - GexinSdkDelegate
- (void)GexinSdkDidRegisterClient:(NSString *)clientId
{
// [4-EXT-1]: 个推SDK已注册
_sdkStatus = SdkStatusStarted;
_clientId = clientId;
[_viewController updateStatusView:self];
//    [self stopSdk];
}
- (void)GexinSdkDidReceivePayload:(NSString *)payloadId fromApplication:(NSString *)appId
{
// [4]: 收到个推消息
_payloadId = payloadId;
NSData *payload = [_gexinPusher retrivePayloadById:payloadId];
NSString *payloadMsg = nil;
if (payload) {
payloadMsg = [[NSString alloc] initWithBytes:payload.bytes
length:payload.length
encoding:NSUTF8StringEncoding];
}
[_viewController logMsg:payloadMsg];
if (payloadMsg!=nil) {
NSData *jsonData = [payloadMsg dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
NSLog(@"json解析失败:%@",err);
return ;
}
//1. 创建一个plist文件
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *patho=[paths objectAtIndex:0];
NSLog(@"path = %@",patho);
NSString *filename=[patho stringByAppendingPathComponent:@"dataPlist.plist"];
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
//创建一个dic,写到plist文件里
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dic[@"address"],@"address",dic[@"bespeak_time"],@"bespeak_time",dic[@"car_model"],@"car_model",dic[@"id"],@"id",dic[@"car_num"],@"car_num",dic[@"create_time"],@"create_time",dic[@"pay_price"],@"pay_price",dic[@"pay_type"],@"pay_type",dic[@"price"],@"price",dic[@"service_begin_time"],@"service_begin_time",dic[@"service_end_time"],@"service_end_time",dic[@"settlement_time"],@"settlement_time",dic[@"status"],@"status",dic[@"wash_type"],@"wash_type",nil];
[dict writeToFile:filename atomically:YES];
//读文件
NSMutableDictionary* dict2 = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"dic is:%@",dict2);
}
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}
- (void)GexinSdkDidSendMessage:(NSString *)messageId result:(int)result {
// [4-EXT]:发送上行消息结果反馈
NSString *record = [NSString stringWithFormat:@"Received sendmessage:%@ result:%d", messageId, result];
[_viewController logMsg:record];
}
- (void)GexinSdkDidOccurError:(NSError *)error
{
// [EXT]:个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。
[_viewController logMsg:[NSString stringWithFormat:@">>>[GexinSdk error]:%@", [error localizedDescription]]];
}
@end
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
objective c
传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
iphone下,如何获取对象的句柄和其父对象的句柄?
ios 如何隐藏状态栏
iOS 第6项目个别技术点
iOS 调用地图导航
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服