Tích hợp AdBrix Growth Action [iOS - Swift]
Theo dõi
Thông tin cần biết
Để sử dụng Growth Action thì cần tích hợp common sdk trước.
Tích hợp AdBrix IOS SDK [Swift]]
[[인용:위험:보통]] Dịch vụ Growth Action iOS chỉ hỗ trợ trên thiết bị tối thiểu iOS 10.
[[인용:안내:보통]] Để sử dụng Growth Action
_ Growth Action là giải pháp cung cấp gửi Push Notification tự động theo Scenario và targeting audience.[Chi tiết]
_ Growth Action là premium add-on mở rộng, để sử dụng tính năng này cần sử dụng gói Paid Pricing Plan. [Service Request]
Cấu hình Growth Action cơ bản
Đăng kí thông tin APNS push certificate
Cần vào Apple Developer để lấy thông tin APNS push token key hoặc certificate, đăng kí với AdBrix.
_ Vào AdBrix Console, Growth Action - Settings - iOS Push Setting để nhập thông tin
[[인용:경고:보통]] Chú ý.
_ Nên sử dụng phương thức xác thực bằng APNS Token Key (p8). Nếu sử dụng phương thức xác thực bằng p12 certificate thì phải chọn p12 không có mật khẩu.
_ p12 certificate phải chọn loại hỗ trợ Production.
_ iOS Growth Action chỉ hỗ trợ Production Environment.
Hướng dẫn nhận APNS p8
- Vào Member Center
- Tạo
Key
cho push notification. Tải file.p8
- Ghi lại giá trị
key id
- Ghi lại giá trị
team id
ở Account Membership
Hướng dẫn nhận APNS Certificate (p12)
- Đăng nhập vào https://developer.apple.com/membercenter/index.action.
- Nhấp Certificates, Identifiers & Profiles.
- Chọn App ID trong danh sách đổ xuống. Bạn phải tạo App ID từ trước.
- Chọn loại Apple Push Notification service SSL (Sandbox & Production)
Cấu hình Capability trong XCode
- Di chuyển tới Signing & Capabilities -> Capability
- Trong mục Background Modes, nhấp Remote Notifications.
- Thêm Capability: Push Notifications như hình bên dưới
Bổ sung framework cần thiết
- Trong hạng mục General, bổ sung NotificationCenter.framework, UserNotifications.framework.
Cài đặt Notification Service Extension
Cần cài đặt Notification Service Extension để có thể nhận được remote notification.
a. Nhấp vào nút '+' ở dưới Xcode
b. Chọn Notification Service Extension, rồi bấm Next
c. Nhập Product Name
d. Trong mục General của Extension, thêm AdBrixRM.framework.
Framework này hiện tại nằm trong thư mục Pods -> AdBrixRematered. Kéo và thả framework từ thư mục Pods vào Frameworks and Libraries
e. Sau khi tạo xong Extension, mở file được tạo tự động NotificationService.swift thay đổi như sau:
import UserNotifications import AdBrixRM class NotificationService : AdBrixPushService {}
Cập nhật AppDelegate
a. Gửi Device Token về AdBrix
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){ let adBrix = AdBrixRM.getInstance adBrix.setRegistrationId(deviceToken: deviceToken) }
b. Trong hàm didFinishLaunchingWithOptions, tùy vào sự đồng ý cho phép gửi/nhận push notification, gọi hàm cấu hình tính năng push On / Off
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { ... let unCenter = UNUserNotificationCenter.current() unCenter.delegate = self let options : UNAuthorizationOptions = [.alert,.sound,.badge] unCenter.requestAuthorization(options: options, completionHandler: {(granted,error) in if granted{ DispatchQueue.main.async { // If Push permission is granted enable AdBrix Push AdBrixRM.getInstance.setPushEnable(toPushEnable: true) application.registerForRemoteNotifications() UIApplication.shared.registerForRemoteNotifications() } } else{ // If Push permission is not granted disable AdBrix Push AdBrixRM.getInstance.setPushEnable(toPushEnable: false) } }) ... }
[[인용:경고:보통]]Chú ý về giá trị PushEnable mặc định
Giá trị mặc định là 'Push off'. Vì thế, phải sử dụng hàm trên để thay đổi giá trị 'Push On' để thiết bị có thể nhận tin nhắn.
Thời điểm tốt nhất để gọi hàm này là sau khi nhận được "permission" từ phía người dùng cho phép gửi/nhận push message.
c. Thêm hàm tracking người dùng mở app từ push notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { AdBrixRM.getInstance.userNotificationCenter(center: center, response: response) completionHandler() }
[[인용:안내:보통]] Xin chúc mừng bạn đã tích hợp xong dịch vụ Growth Action~!
Chúng ta đã hoàn thành bước tích hợp cơ bản cho phép AdBrix Server gửi Push Notification cho người dùng. Ngoài ra, để sử dụng các hàm tạo local notification, hãy tham khảo các hàm bên dưới thêm.
Growth Action cài đặt bổ sung
Push Notification Deligate
Cấu hình local và remote notification delegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, AdBrixRmPushLocalDelegate, AdBrixRmPushRemoteDelegate { ... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { ... adBrix.setAdBrixRmPushLocalDelegate(delegate: self) // Local Push Delegate adBrix.setAdBrixRmPushRemoteDelegate(delegate: self) // Reomte Push Delegate ... } // Local Push Delegate func pushLocalCallback(data: Dictionary<String, Any>?, state: UIApplication.State) { print("Local Push Received") } // Reomte Push Delegate func pushRemoteCallback(data: Dictionary<String, Any>?, state: UIApplication.State) { print("Remote Push Received") } ... }
[[인용:위험:보통]] Sau khi người dùng nhấp vào notification và app vào trạng thái foreground thì delegate hoạt động.
Local Push Setting
Sử dụng local push
Ví dụ về việc tạo local push notification
@IBAction func localPushEvent(_ sender: Any) { let calendar = Calendar.current let alarmTime = calendar.date(byAdding: .second, value: 5, to: Date()) let notisound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "mySound.aiff")) AdBrixRM.getInstance.registerLocalPushNotification(id: "12345", date: alarmTime!, title: "this is Title", subtitle: "this is subtitle", body: "This is text", sound: notisound, categoryId: nil, threadId: nil, badgeNumber: 3, image:URL(string: "https://myURL.com/myImage.jpg"), attrDic: ["customKey" : "customValue"], completionHandler: {(isRegSucc,error,pushid)in print("registerLocalPushnotification is RegSucc:: \(isRegSucc) : \(pushid)") }) }
Giá trị tham số
- id : unique ID của notification
- date : thời gian hiển thị notification
- title : tiêu đề
- subtitle : tiêu đề phụ
- body : nội dung notification
- sound : cài đặt âm thanh (nếu để nil thì sử dụng âm thanh mặc định)
- categoryId : mã ID của category
- threadId : thread ID
- badgeNumber : giá trị badge ở icon
- image : ảnh local trong app hoặc trên internet (trường hợp dùng ảnh trên internet thì phải dùng https:// )
- attrDic : tham số meta-data dạng Dictionary
- completionHandler : callback handler
- isRegSucc : kết quả đăng kí notification
- error : khi có lỗi xảy ra lúc tạo notification, (khi thành công thì giá trị là nil)
- pushId : ID của push đã được tạo
Cancel local push
Hủy việc hiển thị local push đang trong trạng thái đợi
//Current Local Push List AdBrixRM.getInstance.getRegisteredLocalPushNotification(completeHandler: {(idArr) in print(“myLocalPushList : \(idArr)“) }) // Cancel the certain Local Push let arr : Array = ["pushid1"] AdBrixRM.getInstance.cancelLocalPushNotification(ids: arr) // Cancel all Local Push AdBrixRM.getInstance.cancelLocalPushNotificationAll()
- getRegisteredLocalPushNotification : mảng chứa danh sách local push trong hàng đợi
- cancelLocalPushNotification : sử dụng push ID để hủy một notification nhất định
- cancelLocalPushNotificationAll : hủy toàn bộ local push
Bình luận
0 bình luận
Bài viết bị đóng bình luận.