iOS JPush简易使用

第一步:进入开发者的网站



用app的Bundle Identifier注册一个App ID···


此时拖到最后要选择Push

注册一个ID之后注册证书

证书也要选择push

继续下一步需要选择一个CSA文件

我们需要从钥匙串请求

继续下一步

存储到本地成功

这时在证书处选择该文件 继续下一步 证书下载 下载成功.cer文件
双击打开 在钥匙串中导出该文件

导出为.p12文件即可

此时在极光需要证书的地方上传上去即可。

另外,在Xcode中需要设置的地方

在AppDelegate.m中
#define JPushKey @"........."
设置两个属性

@property NSData * deviceToken;

@property NSString * noticeType;

然后重写didFinishLaunching方法将后面的代码粘贴上去即可尝试推送。。。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    static NSString *jPushChannel = @"Publish channel";
    static BOOL isProduction = TRUE;
    //初始化
    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:JPushKey channel:jPushChannel apsForProduction:isProduction];
    
    NSString * userInfoJson =
    [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfoJson"];
    //    if (userInfoJson){
    NSLog(@"userInfoJson from out : %@", userInfoJson);
    //    }
    //
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userInfoJson"];
    if (launchOptions){
        NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        //这个判断是在程序没有运行的情况下收到通知,点击通知跳转页面
        if (remoteNotification) {
            // remoteNotification 就是userInfo
            
            [self networkDidReceiveMessage: remoteNotification];
        }
    }
    
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
    [JPUSHService resetBadge];
   

    return YES;
}
- (void)networkDidReceiveMessage: (NSDictionary *) userInfo{
    
    // 这里判断一下userInfo里的消息,以决定使用哪种类型的页面。
    NSError *error;
    
    NSData *userInfoJsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:&error];
    if (error){
        NSLog(@"error : %@" , [error localizedDescription]);
        return;
    }
    if (!userInfoJsonData){
        
        NSLog(@"error : %@" , [error localizedDescription]);
        return;
    }
    NSString *jsonString = [[NSString alloc] initWithData:userInfoJsonData encoding:NSUTF8StringEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:jsonString forKey:@"userInfoJson"];
    
}


// =============================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];
    _deviceToken = deviceToken;
}

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

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings {
    NSLog(@"bbb");
    [application registerForRemoteNotifications];
}
// 获取服务端的通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *) userInfo {
    
    
    NSLog(@"*********");
    
    //[application setApplicationIconBadgeNumber:0];
    //[application cancelAllLocalNotifications];
    //[JPUSHService resetBadge];
    
    NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    
    [JPUSHService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult)) completionHandler {
    
    [JPUSHService handleRemoteNotification: userInfo];
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"openWeb" object:nil];
    
    NSLog(@"通知信息%@",userInfo);
    
    [application setApplicationIconBadgeNumber:0];
    [application cancelAllLocalNotifications];
    [JPUSHService resetBadge];
    
    NSNotification *notification = [[NSNotification alloc] initWithName:kJPFNetworkDidReceiveMessageNotification object:nil userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    NSLog(@"………………………………");
    self.noticeType = (NSString *)[userInfo objectForKey:@"noticeType"];
        if (self.noticeType == nil) {
            
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

// 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");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值