zoukankan      html  css  js  c++  java
  • 第三方-激光推送2.1.0

    1.首先创建一个类目

    #import "AppDelegate+JPush.h"
    #import "JPUSHService.h"
    #import "WJNotifier.h"//依赖文件
    
    static NSString *JIGUANG_PUSH_KEY = @"4cdcd543c073753fb8932246";//key
    
    //NSString *_tag = nil;
    @implementation AppDelegate (JPush)
    
    
    
    - (void)initJpush:(NSDictionary *)launchOptions{
        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:JIGUANG_PUSH_KEY channel:@"0" apsForProduction:YES];
    }
    
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        
        // Required
        [JPUSHService registerDeviceToken:deviceToken];
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        
        // Required,For systems with less than or equal to iOS6
        [JPUSHService handleRemoteNotification:userInfo];
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
        
        // IOS 7 Support Required
        [JPUSHService handleRemoteNotification:userInfo];
        completionHandler(UIBackgroundFetchResultNewData);
        if (application.applicationState == UIApplicationStateActive) {
            //音效播放在里面
            [WJNotifier showNotifer:userInfo[@"aps"][@"alert"]];
        }
        [CoreManager setJPushNumber];
        [[NSNotificationCenter defaultCenter]postNotificationName:notice_jpush object:@"1"];
    }
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        
        //Optional
        NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
    }
    
    @end

    2.在下面方法里面调用初始化

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

    3.在根控制器中监听状态

    - (void)dealloc {
        [self unObserveAllNotifications];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter addObserver:self
                          selector:@selector(networkDidSetup:)
                              name:kJPFNetworkDidSetupNotification
                            object:nil];
        [defaultCenter addObserver:self
                          selector:@selector(networkDidClose:)
                              name:kJPFNetworkDidCloseNotification
                            object:nil];
        [defaultCenter addObserver:self
                          selector:@selector(networkDidRegister:)
                              name:kJPFNetworkDidRegisterNotification
                            object:nil];
        [defaultCenter addObserver:self
                          selector:@selector(networkDidLogin:)
                              name:kJPFNetworkDidLoginNotification
                            object:nil];
        [defaultCenter addObserver:self
                          selector:@selector(networkDidReceiveMessage:)
                              name:kJPFNetworkDidReceiveMessageNotification
                            object:nil];
        [defaultCenter addObserver:self
                          selector:@selector(serviceError:)
                              name:kJPFServiceErrorNotification
                            object:nil];
        
    }
    
    
    - (void)unObserveAllNotifications {
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter removeObserver:self
                                 name:kJPFNetworkDidSetupNotification
                               object:nil];
        [defaultCenter removeObserver:self
                                 name:kJPFNetworkDidCloseNotification
                               object:nil];
        [defaultCenter removeObserver:self
                                 name:kJPFNetworkDidRegisterNotification
                               object:nil];
        [defaultCenter removeObserver:self
                                 name:kJPFNetworkDidLoginNotification
                               object:nil];
        [defaultCenter removeObserver:self
                                 name:kJPFNetworkDidReceiveMessageNotification
                               object:nil];
        [defaultCenter removeObserver:self
                                 name:kJPFServiceErrorNotification
                               object:nil];
    }
    
    - (void)networkDidSetup:(NSNotification *)notification {
        NSLog(@"已连接");
    }
    
    - (void)networkDidClose:(NSNotification *)notification {
    
    }
    
    - (void)networkDidRegister:(NSNotification *)notification {
        NSLog(@"%@", [notification userInfo]);
        NSLog(@"已注册");
    }
    
    - (void)networkDidLogin:(NSNotification *)notification {
        NSLog(@"已登录");
        if ([JPUSHService registrationID]) {
            NSLog(@"get RegistrationID");
        //转入别名 [self resetAliasAndTagWithTag:[UserManager sharedUserInfoWithPlist].name]; } }
    - (void)networkDidReceiveMessage:(NSNotification *)notification { } - (void)serviceError:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *error = [userInfo valueForKey:@"error"]; NSLog(@"%@", error); } - (void)resetAliasAndTagWithTag:(NSString *)tag { __autoreleasing NSMutableSet * tags = [NSMutableSet set]; if (tag == nil) { [tags addObject:@""]; }else { [tags addObject:tag]; } //没有直接传入@"" [JPUSHService setTags:[NSSet set] alias:tag callbackSelector:@selector(tagsAliasCallback:tags:alias:) object:self]; } - (void)tagsAliasCallback:(int)iResCode tags:(NSSet *)tags alias:(NSString *)alias { NSLog(@"%d",iResCode); NSLog(@"标签设置成功,标签名为%@",alias); switch (iResCode) { case 6002: [self resetAliasAndTagWithTag:[UserManager sharedUserInfoWithPlist].name]; break; default:; } }
  • 相关阅读:
    POJ 2749
    POJ 3422
    POJ 3621
    SQLSERVER 2005 重新安装过程中的疑难解决
    可遇不可求的Question之MySqlClient访问字段返回System.Byte[]篇
    可遇不可求的Question之odbc驱动无法加载
    可遇不可求的BUG之采用MYSQL odbc 3.51访问数据库返回值缺失
    可遇不可求的Bug之Convert.Int32(<未定义的值>)等于0
    可遇不可求的Question之数据库操作超时篇
    可遇不可求的Question之数据库 'tempdb' 的日志已满。
  • 原文地址:https://www.cnblogs.com/hxwj/p/5141661.html
Copyright © 2011-2022 走看看