极光推送浅解

1.去极光推送官网注册账号

2.在苹果开发网注册推送证书,还有描述文件

3.回到钥匙串将证书导出p12格式

4.回到极光推送创建应用

5.下载SDK导入工程

6.在工程中加入以下类:

   (1)CFNetwork.framework

   (2)CoreFoundation.framework

   (3)CoreTelephony.framework

   (4)SystemConfiguration.framework

   (5)CoreGraphics.framework

   (6)Foundation.framework

   (7)UIKit.framework

   (8)Security.framework

   (9)libz.tbd

   (10)Adsupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)

7.在plist文件中加入
 <key>NSAppTransportSecurity</key> 
  <dict> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
  </dict> 
8.如果需要声音,将音频格式转换为caf格式
9.Bundle identifier 中设置bundle ID
10
11.

#import <AVFoundation/AVFoundation.h>

#import "AppDelegate.h"

#import "JPUSHService.h"

#import "ViewController.h"

@interface AppDelegate ()

@property(nonatomic, strong) AVAudioPlayer *audioPlayer;

@end

@implementation AppDelegate

#warning 应用程序启动后,要执行的委托调用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    //创建一个RootViewController对象

    ViewController *rootVC = [[ViewController alloc] init];

    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];

    UITabBarController *tabbarVC = [[UITabBarController alloc] init];

    tabbarVC.viewControllers = @[naVC];

    //window指定根视图控制器

    self.window.rootViewController = tabbarVC;

    

    

    //    1.注册远程推送

    //    1.1设置配置选项

    //    1.2注册

    //    2.获取deviceToken,并发送给服务器

    //    3.接收远程推送通知

    //    注意:2,3步通过协议方法完成

    //    1.注册

    //    1.1设置推送选项

    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge| UIUserNotificationTypeSound| UIUserNotificationTypeAlert) categories:nil];

    

    /*!

     * @abstract 启动SDK

     *

     * @param launchingOption 启动参数.

     * @param appKey 一个JPush 应用必须的,唯一的标识. 请参考 JPush 相关说明文档来获取这个标识.

     * @param channel 发布渠道. 可选.

     * @param isProduction 是否生产环境. 如果为开发状态,设置为 NO; 如果为生产状态,应改为 YES.

     * @param advertisingIdentifier 广告标识符(IDFA 如果不需要使用IDFA,传nil.

     *

     * @discussion 提供SDK启动必须的参数, 来启动 SDK.

     * 此接口必须在 App 启动时调用, 否则 JPush SDK 将无法正常工作.

     */

    

//  1.2注册

    [JPUSHService setupWithOption:launchOptions appKey:@"db43c773ba7db0917d1940be"

                          channel:@"Publish channel"

                 apsForProduction:NO

            advertisingIdentifier:@""];

    

    

//**************下面是当前app不在活跃状态(关闭应用)时处理推送通知********

//声明一个字典接收打开应用时收到的通知

    NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    NSLog(@"收到通知 === :%@", remoteNotification);

    

    if (application.applicationState ==  UIApplicationStateInactive) {

        

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            //执行跳转功能

            [self handleNotificationAction:remoteNotification];

        });

    }

    return YES;

}

//2.获取deviceToken的协议方法

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    

    NSLog(@"====%@",deviceToken);

//手机运行获取token传给极光服务器

    [JPUSHService registerDeviceToken:deviceToken];

    

}

//接收到极光推送过来的消息

//此方法如果App处于后台,当用户点击<推送通知>时会执行该方法

//另一种注释:如果远程消息发送过来的时候,app正在运行,这时候会发生什么呢?

//  app代理的application:didReceiveRemoteNotification:方法会被调用,同时远程消息中的payload数据会作为参数传递进去。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    

    NSLog(@"userInfo = %@",userInfo);

    //处理收到的anps消息 不写会收不到消息

    [JPUSHService handleRemoteNotification:userInfo];

    //记录新数据

    completionHandler(UIBackgroundFetchResultNewData);

//    NSLog(@"收到通知2: = %@",userInfo);

    //设置badge角标数字为零

//    application.applicationIconBadgeNumber = 0;

    

//加个判断

    if (application.applicationState ==  UIApplicationStateActive) {

        //将取得的值发给下面的方法

        [self handleNotificationAction:userInfo];

    }else{

        return;

    }

    

}

//自定义通知方法

-(void)handleNotificationAction:(NSDictionary *)dic

{

//    NSString *string = [dic objectForKey:@"ViewController"];

    //通过字符串得到一个类

//    Class vcClass = NSClassFromString(string);

    //通过类创建对象

//    id vcObject = [[[vcClass class] alloc] init];

    

   //取到相应的值发送给首页

    NSString *str = [dic objectForKey:@"tong"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:str userInfo:nil];

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    //Optional

    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);

}

#warning 应用程序将要由活动状态切换到非活动状态时执行的委托调用,如按下home 按钮,返回主屏幕,或全屏之间切换应用程序等。

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

}

#warning 在应用程序已进入后台程序时,要执行的委托调用。所以要设置后台继续运行,则在这个函数里面设置即可。

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

}

#warning 在应用程序将要进入前台时(被激活),要执行的委托调用,与applicationWillResignActive 方法相对应。

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

}

#warning 在应用程序已被激活后,要执行的委托调用,刚好与  applicationDidEnterBackground 方法相对应。

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

}

#warning 在应用程序要完全退出的时候,要执行的委托调用。

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

需要给系统服务器传别名的可以用方法:

[JPUSHService setAlias:[NSString stringWithFormat:@"%@",telTF.text] callbackSelector:nil object:self];

字符串是你设置的别名.


                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值