IOS統合JPush

この記事では、Xcodeの手動統合を使用していますJPush

証明書

参考サイト:https://docs.jiguang.cn//jpush/client/iOS/ios_cer_guide/

ダウンロードSDK

ダウンロードURL:https://docs.jiguang.cn//jpush/resources/
image.png

  • SDKディレクトリ
    image.png

インポートSDK

  • .Aファイルのインポートプロジェクト内のlib .hファイル
    image.png
  • .hファイル
    image.png
  • ファイル.A
    image.png

手動インポート

  • フレームの追加
    CFNetwork.framework
    CoreFoundation.framework
    CoreTelephony.framework
    SystemConfiguration.framework
    CoreGraphics.framework
    Foundation.framework
    UIKit.framework
    Security.frameworkの
    libz.tbd(Xcodeのバージョン7され、次のlibz.dylib)
    AdSupport.frameworkを(IDFAが必要な取得、あなたが使用していない場合IDFAは、)を追加しないでください
    UserNotifications.framework(Xcodeの8以上)
    libresolv.tbd(JPush 2.2.0および必要性の上に、Xcodeのを7次のバージョンがありlibresolv.dylib)

設定を構築

如果你的工程需要支持小于7.0的iOS系统,请到构建设置关闭bitCode选项,否则将无法正常编译通过。
设置搜索路径下的用户头搜索路径和库搜索路径,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为“$(SRCROOT)/ {静态库所在文件夹名称} “即可。

功能

如使用Xcode 8及以上环境开发,请开启Application Target的功能 - >推送通知选项,如图: image.png

允许Xcode 7支持Http传输方法

  • 在项目的info.plist中添加一个Key:NSAppTransportSecurity,类型为字典类型。
  • 然后给它添加一个NSExceptionDomains,类型为字典类型;
  • 把需要的支持的域添加给NSExceptionDomains。其中jpush.cn作为Key,类型为字典类型。
  • 每个域下面需要设置2个属性:NSIncludesSubdomains,NSExceptionAllowsInsecureHTTPLoads。两个属性均为布尔类型,值分别为YES,YES。image.png

    代码配置

    添加头文件

  • 将以下代码添加到AppDelegate.m引用头文件的位置。
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  • 添加代表
@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

添加初始化APNs代码

  • 请将以下代码添加到 - (BOOL)应用程序:(UIApplication )应用程序didFinishLaunchingWithOptions:(NSDictionary )launchOptions
  //Required
  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
  if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    // 可以添加自定义 categories
    // NSSet<UNNotificationCategory *> *categories for iOS10 or later
    // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
  }
  [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];

添加初始化JPush代码

  • 将以下代码添加到 - (BOOL)应用程序:(UIApplication )应用程序didFinishLaunchingWithOptions:(NSDictionary )launchOptions
  // Required
  // init Push
  // notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
  // 如需继续使用 pushConfig.plist 文件声明 appKey 等配置内容,请依旧使用 [JPUSHService setupWithOption:launchOptions] 方式初始化。
  [JPUSHService setupWithOption:launchOptions appKey:@"你的appkey"
                        channel:@"Publish channel"
               apsForProduction:NO
          advertisingIdentifier:nil];
  • 部分参数说明:

  • APPKEY
    • 選択したWeb上のアプリケーションポータルを、そのAPPKEY値の「設定」をクリックしてください。アプリケーションAPPKEYとポータルAPPKEY一貫した内で生成されたアプリケーションの構成を作成してください。
  • チャンネル
    • サブチャネル統計を容易にするため、アプリケーションパッケージに指定されたチャンネルをダウンロードし、特定の値自体はなど、あなたによって定義されています:App Storeで。
  • apsForProduction
    • 新しいの1.3.1バージョンは、のAPNs証明書のアプリケーションによって使用される現在の環境を識別します。
    • 0(デフォルト値)の表現は、開発証明書であり、1は生産証明書発行アプリケーションの使用を示しています。
    • 注意:このフィールドの一貫性のある値は、コード署名ビルド設定証明書の環境で構成します。
  • advertisingIdentifier

APNは正常に登録してDeviceTokenを報告しました

  • コールバックメソッドのAppDelegate.mを実装するには、コードのコールバックメソッドを追加します
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

  /// Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}

インタフェースは、登録のAPNを達成するために失敗しました

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

追加処理のAPN通知コールバックメソッド

  • コールバックメソッドのAppDelegate.mを実装するには、コードのコールバックメソッドを追加します
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //从通知界面直接进入应用
  }else{
    //从通知设置界面进入应用
  }
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // Required
  NSDictionary * userInfo = notification.request.content.userInfo;
  if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
  // Required
  NSDictionary * userInfo = response.notification.request.content.userInfo;
  if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
  }
  completionHandler();  // 系统要求执行这个方法
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

  // Required, For systems with less than or equal to iOS 6
  [JPUSHService handleRemoteNotification:userInfo];
}

成功した操作

  • オンデバイスプロジェクトのデバッグ、以下の場合は、コンソール出力があなたに代わって成功裏に統合されているログ。
2016-08-19 17:12:12.745823 219b28[1443:286814]  | JPUSH | I - [JPUSHLogin]
----- login result -----
uid:5460310207
registrationID:171976fa8a8620a14a4

プッシュメッセージ

前の記事を参照してください(JAVA統合JPush):https://blog.csdn.net/weixin_41182727/article/details/98934176

おすすめ

転載: www.cnblogs.com/maggieq8324/p/11414851.html