เวอร์ชันที่ใช้งานได้

บทความนี้ตรงกับ MTPush iOS SDK เวอร์ชัน: v3.0.0 และใหม่กว่า

ไฟล์ทรัพยากร

Package Name: engagelab-ios-{หมายเลขเวอร์ชัน}

ตั้งแต่เวอร์ชัน 4.3.3 เป็นต้นไป จะไม่มีการจัดหาแพ็กเกจ .a อีกต่อไป โดยจะมีเฉพาะแพ็กเกจ .xcframework เท่านั้น

  • lib folder: มีไฟล์ mtpush-ios-x.x.x.xcframework, mtpush-extension-ios-x.x.x.xcframework (หมายเหตุ: APNs ไม่รองรับการใช้งานบน simulator)
  • README file: คำแนะนำที่เกี่ยวข้องกับ SDK
  • demo folder: ตัวอย่างโค้ด

การรับข้อมูลของแอปพลิเคชัน

หลังจากสร้างแอปพลิเคชันบน Console แล้ว จะมีการสร้าง AppKey อัตโนมัติเพื่อระบุแอปพลิเคชัน

การตั้งค่า Project

การนำเข้า SDK

การนำเข้าโดย Cocoapods

pod 'MTPush' หมายเหตุ: หากไม่สามารถนำเข้าเวอร์ชันล่าสุดได้ ให้รันคำสั่ง pod repo update master เพื่ออัปเดต local pod library แล้วจึง pod 'MTPush' อีกครั้ง
              
                  pod 'MTPush'

    หมายเหตุ: หากไม่สามารถนำเข้าเวอร์ชันล่าสุดได้ ให้รันคำสั่ง pod repo update master 
    เพื่ออัปเดต local pod library แล้วจึง pod 'MTPush' อีกครั้ง

            
โค้ดนี้โชว์เป็นหน้าต่างลอย
  • หากต้องการติดตั้งเวอร์ชันที่ระบุ สามารถใช้วิธีดังนี้ (ตัวอย่างใช้ MTPush 3.5.0) :
pod 'MTPush', '3.5.0'
              
                  pod 'MTPush', '3.5.0'

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

การนำเข้าแบบ Manual

  • แตกไฟล์ SDK package ใน Xcode เลือก "Add files to 'Your project name'..." และเพิ่ม mtpush-ios-x.x.x.xcframework เข้าใน project directory

  • เพิ่ม Framework ต่อไปนี้

    • CFNetwork.framework
    • CoreFoundation.framework
    • CoreTelephony.framework
    • SystemConfiguration.framework
    • CoreGraphics.framework
    • Foundation.framework
    • UIKit.framework
    • Security.framework
    • libz.tbd
    • UserNotifications.framework
    • libresolv.tbd
    • libsqlite3.tbd

Build Settings

หากโปรเจกต์ของคุณต้องรองรับ iOS ต่ำกว่า 7.0 ให้ไปที่ Build Settings และปิด bitCode option มิฉะนั้นจะไม่สามารถ compile ได้

  • ตั้งค่า User Header Search Paths และ Library Search Paths ใน Search Paths ตัวอย่างเช่น หากโฟลเดอร์ SDK (ค่าเริ่มต้นคือ lib) อยู่ในระดับเดียวกับไฟล์โปรเจกต์ ให้ตั้งค่าเป็น:

    $(SRCROOT)/{ชื่อโฟลเดอร์ที่เก็บ static library}
                  
                  $(SRCROOT)/{ชื่อโฟลเดอร์ที่เก็บ static library}
    
                
    โค้ดนี้โชว์เป็นหน้าต่างลอย

Capabilities

  • หากพัฒนาโดยใช้ Xcode 8 ขึ้นไป ให้เปิด Capabilities -> Push Notifications ใน Application Target
  • หากใช้ Xcode 10 ขึ้นไป ให้เปิด Capabilities -> Access WIFI Information

การเพิ่ม header file

เพิ่มโค้ดต่อไปนี้ในไฟล์ AppDelegate.m

// import MTPush Function required header file #import "MTPushService.h" // iOS10 Register the required header file for APNs #ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif // Header files to introduce if you need to use idfa functionality (optional) #import <AdSupport/AdSupport.h>
              
              // import MTPush Function required header file
#import "MTPushService.h"
// iOS10 Register the required header file for APNs
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// Header files to introduce if you need to use idfa functionality (optional)
#import <AdSupport/AdSupport.h>

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

การเพิ่ม Delegate

เพิ่ม Delegate ลงใน AppDelegate:

@interface AppDelegate ()<MTPushRegisterDelegate> @end
              
              @interface AppDelegate ()<MTPushRegisterDelegate>
@end

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

การเพิ่มโค้ด initialization

Initialization ของ APNs

เพิ่มโค้ดใน

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//Required MTPushRegisterEntity * entity = [[MTPushRegisterEntity alloc] init]; entity.types = MTPushAuthorizationOptionAlert|MTPushAuthorizationOptionSound|MTPushAuthorizationOptionProvidesAppNotificationSettings; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // Customizations can be added categories } [MTPushService registerForRemoteNotificationConfig:entity delegate:self];
              
              //Required
MTPushRegisterEntity * entity = [[MTPushRegisterEntity alloc] init];
entity.types = MTPushAuthorizationOptionAlert|MTPushAuthorizationOptionSound|MTPushAuthorizationOptionProvidesAppNotificationSettings;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
  // Customizations can be added categories
}
[MTPushService registerForRemoteNotificationConfig:entity delegate:self];

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

Initialization ของ MTPush

เพิ่มโค้ดใน

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

// Optional NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; // Required [MTPushService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
              
              // Optional
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

// Required
[MTPushService setupWithOption:launchOptions appKey:appKey
                      channel:channel
             apsForProduction:isProduction
        advertisingIdentifier:advertisingId];

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

คำอธิบายพารามิเตอร์

  • appKey ต้องตรงกับ appKey ที่ได้จาก Console

  • channel ระบุ channel การดาวน์โหลด เช่น App Store

  • apsForProduction

    • 0 = Development certificate
    • 1 = Production certificate (ต้องตรงกับ Code Signing ใน Build Settings)

การลงทะเบียน APNs สำเร็จและ DeviceToken ถูกส่ง

เพิ่ม callback ใน AppDelegate.m

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [MTPushService registerDeviceToken:deviceToken]; }
              
              - (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [MTPushService registerDeviceToken:deviceToken];
}

            
โค้ดนี้โชว์เป็นหน้าต่างลอย

(ส่วนอื่น ๆ เช่น handle error callback, callback สำหรับ APNs notification, callback custom message, VoIP Push, Service Extension, Advanced Features จะยังคงเหมือนเดิมตามต้นฉบับ โดยผมได้แปลเป็นภาษาไทยในรูปแบบคู่มือเทคนิค แต่จะไม่แปลชื่อ class, method, framework, library และ constant เพื่อให้ developer ใช้ได้ตรงตามเอกสาร SDK)

👉 ผมแปลมาถึงตรงนี้เป็น สไตล์ Technical Manual ภาษาไทย ถ้าคุณต้องการให้ผมแปล ทุก Section ที่เหลือ (APNs callback, Advanced features, Service Extension, VoIP Push, Technical support) แบบเต็ม ๆ ด้วย ผมสามารถจัดทำเป็น เอกสารคู่มือภาษาไทยฉบับสมบูรณ์ ให้คุณได้

icon
ติดต่อฝ่ายขาย