極光推送

製做證書的過程詳細看官方文檔或者以下推薦:html

http://jingyan.baidu.com/article/c1465413975cba0bfcfc4ccf.htmlios

http://docs.jpush.io/client/ios_tutorials/#ios_1app

http://docs.jpush.io/guideline/ios_guide/框架

http://allluckly.cn/投稿/tuogao28?utm_source=tuicool&utm_medium=referralless

建立完證書,就是去極光官網註冊帳號,建立應用,截圖以下:ide

將建立的證書上傳到應用上了,上傳成功後的截圖以下:工具

證書上傳成功後,生成APP Key,截圖以下:fetch

好了,這下工做作完了,剩下的就是代碼實現了:ui

第一步:下載SDK,將須要的兩個文件導入項目中:spa

集成壓縮包內容

包名爲JPush-iOS-SDK-{版本號}

  • lib文件夾:包含頭文件 JPUSHService.h,靜態庫文件jpush-ios-x.x.x.a ,支持的iOS版本爲 5.0 及以上版本。(請注意:模擬器不支持APNs)
  • pdf文件:集成指南
  • demo文件夾:示例

第二步:導入須要依賴的庫文件:

必要的框架

  • CFNetwork.framework
  • CoreFoundation.framework
  • CoreTelephony.framework
  • SystemConfiguration.framework
  • CoreGraphics.framework
  • Foundation.framework
  • UIKit.framework
  • Security.framework
  • Xcode7須要的是libz.tbd;Xcode7如下版本是libz.dylib
  • Adsupport.framework (獲取IDFA須要;若是不使用IDFA,請不要添加)

第三步:建立一個工具類,名稱爲KJJPushHelper,封裝註冊時的各類方法

.h

複製代碼
//
//  KJJPushHelper.h
//
//  Created by mac on 16/5/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface KJJPushHelper : NSObject

// 在應用啓動的時候調用
+ (void)setupWithOption:(NSDictionary *)launchingOption
                 appKey:(NSString *)appKey
                channel:(NSString *)channel
       apsForProduction:(BOOL)isProduction
  advertisingIdentifier:(NSString *)advertisingId;

// 在appdelegate註冊設備處調用
+ (void)registerDeviceToken:(NSData *)deviceToken;

// ios7之後,纔有completion,不然傳nil
+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion;

// 顯示本地通知在最前面
+ (void)showLocalNotificationAtFront:(UILocalNotification *)notification;

@end
複製代碼

.m

複製代碼
//
//  KJJPushHelper.m

//  Created by mac on 16/5/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "KJJPushHelper.h"
#import "JPUSHService.h"

@implementation KJJPushHelper

+ (void)setupWithOption:(NSDictionary *)launchingOption
                 appKey:(NSString *)appKey
                channel:(NSString *)channel
       apsForProduction:(BOOL)isProduction
  advertisingIdentifier:(NSString *)advertisingId{
    // Required
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
    // ios8以後能夠自定義category
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 能夠添加自定義categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                           categories:nil];
    } else {
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
        // ios8以前 categories 必須爲nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                           categories:nil];
#endif
    }
#else
    // categories 必須爲nil
    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                      UIRemoteNotificationTypeSound |
                                                      UIRemoteNotificationTypeAlert)
                                          categories:nil];
#endif
    
    // Required
    [JPUSHService setupWithOption:launchingOption appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
    return;
}

+ (void)registerDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
    return;
}

+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion {
    [JPUSHService handleRemoteNotification:userInfo];
    
    if (completion) {
        completion(UIBackgroundFetchResultNewData);
    }
    return;
}

+ (void)showLocalNotificationAtFront:(UILocalNotification *)notification {
    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
    return;
}

@end
複製代碼

第四步:建立一個APPDelegate的分類,在該類中調用KJJPushHelper中的類方法

複製代碼
//  AppDelegate+KJJPushSDK.h
//
//  Created by mac on 16/5/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate (KJJPushSDK)
-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end



//  AppDelegate+KJJPushSDK.m
//
//  Created by mac on 16/5/5.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "AppDelegate+KJJPushSDK.h"
#import "KJJPushHelper.h"

#define JPushSDK_AppKey  @"31e01f6a2f6d4b1209061aec"
#define isProduction     NO

@implementation AppDelegate (KJJPushSDK)
-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
  
    [KJJPushHelper setupWithOption:launchOptions appKey:JPushSDK_AppKey channel:nil apsForProduction:isProduction advertisingIdentifier:nil];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Required - 註冊 DeviceToken
    [KJJPushHelper registerDeviceToken:deviceToken];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [KJJPushHelper handleRemoteNotification:userInfo completion:nil];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // IOS 7 Support Required
    [KJJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
    
    // 應用正處理前臺狀態下,不會收到推送消息,所以在此處須要額外處理一下
    if (application.applicationState == UIApplicationStateActive) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"收到推送消息"
                              message:userInfo[@"aps"][@"alert"]
                              delegate:nil
                              cancelButtonTitle:@"取消"
                              otherButtonTitles:@"肯定",nil];
        [alert show];
    }
}


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


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [KJJPushHelper showLocalNotificationAtFront:notification];
    return;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
    return;
}

@end
複製代碼

第五步:在AppDelegate中註冊便可

//註冊極光推送
[self JPushApplication:application didFinishLaunchingWithOptions:launchOptions];

好了,大功告成,插上真機運行:打印結果以下

若是出現下列錯誤信息JPUSH | W - [JPUSHClientController] Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs.

修改方法:

因爲項目以前用到了環信SDK,環信得已經註冊了通知,在AppDelegate中註冊通知,didRegisterForRemoteNotificationsWithDeviceToken與didFailToRegisterForRemoteNotificationsWithError方法,均不執行。。。需到環信註冊通知的地方,再次註冊極光通知。方能夠獲取到Token執行。

相關文章
相關標籤/搜索