博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三方-激光推送2.1.0
阅读量:6151 次
发布时间:2019-06-21

本文共 6477 字,大约阅读时间需要 21 分钟。

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:;    }    }

 

转载于:https://www.cnblogs.com/hxwj/p/5141661.html

你可能感兴趣的文章
前端js之JavaScript
查看>>
Log4J日志配置详解
查看>>
实验7 BindService模拟通信
查看>>
scanf
查看>>
Socket编程注意接收缓冲区大小
查看>>
SpringMVC初写(五)拦截器
查看>>
检测oracle数据库坏块的方法
查看>>
SQL server 安装教程
查看>>
Linux下ftp和ssh详解
查看>>
跨站脚本功攻击,xss,一个简单的例子让你知道什么是xss攻击
查看>>
js时间和时间戳之间如何转换(汇总)
查看>>
js插件---图片懒加载echo.js结合 Amaze UI ScrollSpy 使用
查看>>
java中string和int的相互转换
查看>>
P1666 前缀单词
查看>>
HTML.2文本
查看>>
Ubuntu unity安装Indicator-Multiload
查看>>
解决Eclipse中新建jsp文件ISO8859-1 编码问题
查看>>
7.对象创建型模式-总结
查看>>
【论文阅读】Classification of breast cancer histology images using transfer learning
查看>>
移动端处理图片懒加载
查看>>