`
jackeysion
  • 浏览: 127445 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

iOS应用极光推送接收通知并打开指定功能界面

    博客分类:
  • iOS
阅读更多
一、首先在iOS项目中嵌入极光推送的SDK,具体方法参照极光推送官网,里面涉及一些证书相关的东西,比安卓略显复杂
二、在应用的AppDelegate.m类中,初始化极光推送的sdk,实现接收消息的方法。



#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // ========================================================================
    // 顶部状态栏白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    // ========================================================================
    //
    [DBManager initialDatabases];
    // ========================================================================
    // 百度地图API
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件,请设定     generalDelegate参数
    BOOL ret = [_mapManager start:@"2trUTYe3lbEO527FESyQRwKN"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    // Add the navigation controller's view to the window and display.
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible];
    // 百度地图结束
    // ========================================================================

    // 极光推送API。此处初始化极光推送的SDK
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
    
    // Required
    //如需兼容旧版本的方式,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化和同时使用pushConfig.plist文件声明appKey等配置内容。
    [JPUSHService setupWithOption:launchOptions appKey:jPushAppKey channel:jPushChannel apsForProduction:isProduction];
    
    // ========================================================================
    
    
    return YES;
}


//- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
//{
//    return UIInterfaceOrientationMaskPortrait;
//}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (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.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


// =============================JPush 监听方法======================================================================
/* 
 // 官方建议开发者加上API里面提供下面 5 种类型的通知:
 extern NSString * const kJPFNetworkDidSetupNotification; // 建立连接
 
 extern NSString * const kJPFNetworkDidCloseNotification; // 关闭连接
 
 extern NSString * const kJPFNetworkDidRegisterNotification; // 注册成功
 
 extern NSString * const kJPFNetworkDidLoginNotification; // 登录成功
 */

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"=======");
    NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings {
    NSLog(@"bbb");
}

// Called when your app has been activated by the user selecting an action from
// a local notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *) notification completionHandler:(void (^)())completionHandler {
    NSLog(@"ccccc");
}

// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *) identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    NSLog(@"aaaaa");
}
#endif

// 以下两个方法为接收极光消息的方法,useInfo为接收到的消息,可以打印到控制台看一下。将接收到的消息放到通知中心。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *) userInfo {
    [JPUSHService handleRemoteNotification:userInfo];
    NSLog(@"*********");
    NSLog(@"收到通知:%@", [self logDic:userInfo]);
   
    NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
   
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult)) completionHandler {
    
    [JPUSHService handleRemoteNotification: userInfo];
    
   
    NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
   
    NSLog(@"………………………………");
    NSLog(@"收到通知:%@", [self logDic:userInfo]);
    
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"xxxx");
    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}

// log NSSet with UTF8
// if not ,log will be \Uxxx
- (NSString *) logDic:(NSDictionary *) 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;
}

@end



三、在根视图(我的项目的根视图是TabBarViewController)注册消息通知,成为上面指定消息的观察者

#import <Foundation/Foundation.h>
#import "DWKxbTabBarController.h"

@implementation DWKxbTabBarController

-(void) viewDidLoad{
    [super viewDidLoad];
    self.selectedIndex = 1;
    
   
    // 注册消息通知
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
 
}

// 具体的消息处理方法,比如我这里跳转到另外一个ViewController。
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSLog(@"tabxxxxxxxxxxxx");
    NSDictionary * userInfo = [notification userInfo];
    
    NSLog(@"%@", userInfo);
    
    self.selectedIndex = 1;
    
// 获取tabBar的当前选择视图
    DWLifeNavigationController *life = self.selectedViewController;
    // rootViewController
// 获取NavigationController的根视图(索引为0的child) 
    
    DWColorfulLifeViewController *root = life.childViewControllers[0];
    root.webViewParam = @"foods";
// 根据在storyboard中的连线跳转到指定的ViewController
    [root performSegueWithIdentifier:@"go2SpecialService" sender:root];
}

@end


PS: 第一次做这种功能,mark下来。文章代码里的颜色和粗体不起作用,好桑心
分享到:
评论

相关推荐

    ios原生集成极光推送

    ios原生集成极光推送,sdk已集成,只需要自己配置证书,profile,即可在xcode当中运行

    java接入极光推送demo

    极光推送使用java后台接入demo,推送会通知到Android和Ios的手机通知栏

    ios-极光推送.zip

    请下载官方SDK,导入Lib,申请远程推送证书,在极光后台配置相关信息即可,更多优秀Demo请访问github:https://github.com/mengzhihun6

    JPush极光推送(开发者推送通知和消息应用)V2.1.5安卓版

    极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。平台提供整合了Android推送、iOS推送的统一推送服务。 产品特点 多种推送方式 Push ...

    2017.8 iOS最新版本极光推送demo

    2017.8月通过pod集成极光推送 可以惊醒全推,根据别名推送,根据标签推送,在run本demo时将自己的key替换即可,其他配置均已配置好,在设置别名或标签时可以放在任意界面,为了方便简单我将设置别名和标签放在了...

    ios-极光推送实现页面跳转.zip

    极光推送实现页面跳转

    极光推送SDK For android

    极光推送SDK For android 极光推送平台,使得你可以即时向安装你的应用程序的用户推送通知或者消息,与用户保持互动,从而有效提高留存率,提升用户体验。支持 Android推送、iOS推送。

    极光推送IOS-SDK-1.8.2

    极光推送IOS-SDK-1.8.2,支持armv7,armv7s,arm64版本

    极光推送工具类

    极光推送工具类,包含java端向IOS和android推送设置工具类demo。

    iOS10.0本地推送通知.

    iOS10.0本地推送通知....................................................................................

    极光推送样例代码,里面有详细的注释

    开发者只需在客户端集成极光推送 SDK,即可轻松地添加 Push功能到他的 App中。目前支持 Android 与 iOS。开发者可以在管理Portal上查看推送统计信息,直观地评估推送效果。 编辑本段极光推送服务优势 极光推送服务...

    极光推送---【客户端】、【服务器端】源码.zip

    极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。平台提供整合了Android推送、iOS推送的统一推送服务。

    Python-极光推送官方支持的Python版本服务器端SDK

    极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动,从而有效地提高留存率,提升用户体验。平台提供整合了Android推送、iOS推送的统一推送服务。

    极光推送集成Android Demo

    极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率。极光推送客户端支持 Android,iOS 两个平台...

    关于IOS_APNS推送消息(iphone端+服务端)

    关于IOS_APNS推送消息(iphone端+服务端)。关于IOS_APNS推送消息(iphone端+服务端)。

    iOS推送通知详解

    详细讲解ios推送的中文文档。想用推送的同学可以照着文档做了,保证看得懂。

    解析iOS10中的极光推送消息的适配

    iOS10发布后,发现项目中的极光推送接收消息异常了。 查了相关资料后才发现,iOS10中对于通知做了不少改变。同时也发现极光也很快更新了对应的SDK。 现在就把适配修改的做法分享一下,希望对有需要的童鞋有所帮助。 ...

    自己集成的极光推送内容

    今天新做了一个项目,以前推送的时候用过百度云推送,发现百度云推送到达的太慢,然后使用的是个推进行推送的,发现还是可以吧,然后最近一个项目做IOS的哥们说他集成过极光的,但是集成个推的不太会,好吧,既然都...

    极光推送SDK

    极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能够及时地推送到终端用户手机上,让开发者积极地保持与用户的连接,从而提高用户活跃度、提高应用的留存率。极光推送客户端支持 Android, iOS 两个平台...

    极光推送 自定义源码类

    极光推送的自定义源码类 最简单的直观的方式 一看就懂……

Global site tag (gtag.js) - Google Analytics