导入 JPush推送

1.创建账号,添加项目  获得appkey。

2.导入SDK,导入必须得库.

3.设置 Search Paths 下的 User Header Search Paths 和 Library Search Paths,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为"$(SRCROOT)/[文件夹名称]"即可   (比如桌面的 text项目  就是 $(SRCROOT)/   然后lib文件夹放在哪里就是那里  比如在 text 里面的 text 的lib 的JPush 的话 就是 $(SRCROOT)/text/lib/JPush )

4.创建并配置PushConfig.plist文件

在你的工程中创建一个新的Property List文件,并将其命名为PushConfig.plist,填入Portal为你的应用提供的APP_KEY等参数。

{
APS_FOR_PRODUCTION = “0”
APP_KEY = @“XXXXXXXXXX”
CHANNEL =  @“Publish channel”
}
5.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //以下是JPush极光推送的的必要设置方法
    // Required
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                       UIUserNotificationTypeSound |
                                                       UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
        //categories 必须为nil
        [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                       UIRemoteNotificationTypeSound |
                                                       UIRemoteNotificationTypeAlert)
                                           categories:nil];
    }
#else
    //categories 必须为nil
    [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                   UIRemoteNotificationTypeSound |
                                                   UIRemoteNotificationTypeAlert)
                                       categories:nil];
#endif
    // Required
    NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    NSLog(@"JPushNotification = %@",remoteNotification);
    [APService setupWithOption:launchOptions];
    //以上是JPush极光推送的的必要代理方法

#pragma mark --JPush极光推送的必要代理方法
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    // Required
    [APService registerDeviceToken:deviceToken];
}
<span class="s1" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">//</span><span class="s2" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">接收远程推送通知内容(</span><span class="s1" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">IOS6 </span><span class="s2" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">以下)</span>
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // 取得 APNs 标准信息内容
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
    NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
    NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
    
    // 取得自定义字段内容
    NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
    NSLog(@"在didReceiveRemoteNotification  content =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content,(long)badge,sound,customizeField1);
    
    
    // Required
    [APService handleRemoteNotification:userInfo];
}

<span class="s1" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">//</span><span class="s2" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">接收远程推送通知内容(</span><span class="s1" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">IOS7 </span><span class="s2" style="color: rgb(51, 51, 51); font-family: monospace; font-size: 14px; white-space: pre; background-color: rgb(240, 240, 240);">以上)</span>
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // 取得 APNs 标准信息内容
    NSDictionary *aps = [userInfo valueForKey:@"aps"];
    NSString *content = [aps valueForKey:@"alert"]; //推送显示的内容
    NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge数量
    NSString *sound = [aps valueForKey:@"sound"]; //播放的声音
    
    // 取得自定义字段内容
    NSString *customizeField1 = [userInfo valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
    NSLog(@"在fetchCompletionHandler content =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content,(long)badge,sound,customizeField1);
    
    // IOS 7 Support Required
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}


- (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.
    
    //JPush从后台准备进入前台   未读的红点设置为0
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [APService setBadge:0];
}

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

    

    //JPush从后台重新启动

    [[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];

    [APServicesetBadge:0];

或者   下面是为了防止程序关了后进去
- (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.
    
    //JPush从后台重新启动
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [APService setBadge:0];



6. 然后就在viewcontroller里面上传  registrationid 或者 上传 tags 就可以发送通知了
[APService registrationID];
    NSSet *aaaa = [NSSet setWithObjects:@"zook", nil];
    [APService setTags:aaaa callbackSelector:nil object:nil];

注意:记得在apple developer设置完开发证书之后 在极光的证书要正确导入 而且在项目的capabilities 的 background Modes 的 remote notification 打钩  不然接收不了

还有 关于 push 的数量是在发送通知的时候的 JPush 里面的可选设置的Badge 里面设置
比如+1 就每次+1

还有:发送消息的时候 假如是开发的 记得 推送对象 选择 iOS 开发环境


关于自定义消息发送:

1.自定义消息发送只能在打开应用里面收到,在后台是不能收到的

2.要写进方法

在 AppDelegate 的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
里面
<p class="p1"><pre name="code" class="objc">//自定义消息的接收方法(要打开应用才能收到)
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
</pre><pre name="code" class="objc">
然后方法
<p class="p1"><pre name="code" class="objc">//自定义消息的接收方法(要打开应用才能收到)
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];
    NSString *content = [userInfo valueForKey:@"content"];
    NSDictionary *extras = [userInfo valueForKey:@"extras"];
    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //自定义参数,key是自己定义的
    NSLog(@"content = %@  extras = %@ customizeField1 = %@",content,extras,customizeField1);
    
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值