集成JPush推送

1.JPush官网注册账号,新建应用。上传开发和生产推送证书.p12文件。注册完应用后生成AppKey和Master Secret。客户端只用到AppKey,Master Secret给后台用的。

2.拖到项目中,加依赖库。

3.设置background Modes

4.新建PushConfig.plist,加三个字段。如图。这个plist好像也可以不用。还不确定。。。因为代码中直接就能这三个字段的值了。不用从这读取。


5.复制代码。。。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [IQKeyboardManager sharedManager];

    
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    //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];
    }
    
    //这个不写的话锁屏状态下就收不到消息
     UIUserNotificationType type=UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound;
    UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:type categories:nil];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    //Required
    <span style="color:#ff0000;">填写自己在jpush官网注册的时候得到的key</span>
    NSString *appkey = @"7aaa3f0e9968adc27181ee20";
    [JPUSHService setupWithOption:launchOptions appKey:appkey
                          channel:nil
                 apsForProduction:YES  <span style="color:#ff0000;">发布的时候改为yes表示生产环境</span>
            advertisingIdentifier:advertisingId];
    
    return YES;
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
    
<span style="color:#ff0000;">根据用户名来推送到指定用户 这个方法在登录成功之后也要写一遍。</span>
    if ([Cookie userId]) {
        
        [JPUSHService setTags:[NSSet set] alias:[Cookie userId] fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
            
            
        }];
    }
    
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    <span style="color:#ff0000;">收到推送消息</span>
    NSLog(@"推送内容:%@",userInfo);
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

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


6. APP在前台能收到推送,但是不会像在后台收到推送那样从屏幕顶部弹出提示。这个要根据情况自己处理。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值