- vừa được xem lúc

Push Notifications with Firebase in Swift 5

0 0 81

Người đăng: lehuudung

Theo Viblo Asia

Hôm nay mình sẽ hướng dẫn bạn tạo push notification từ việc tạp appID ,tạo certificate tới việc nhận push bằng cách send Firebase console. Các bước để tích hợp firebase push notification :

Tạo APNs certificate trên Apple’s Developer

Enable push trên Firebase Cloud Messaging Console

Thêm Pod firebase

Viết code push notification

Send push từ Firebase Notification trong dashboard.

I. Tạo file APNs certificate .

Note: Step ngắn gọn để tạo APNs certificate. Ban đầu bạn tạo App ID sẽ chứa bundle ID và tick chọn Push Notifications. Sau khi hoàn thành bước tạo App ID bạn vào mục App ID cuộn xuống phần Push Notifications bạn tạo certificate push.

Bây giờ mình sẽ hướng dẫn bạn chi tiết cách tạo nhé ^_^:

Các bước tạo app id:

  1. Login account develop apple, sau đó chọn mục Certificates, Identifiers & Profiles

  2. Chọn mục Identifiers và Click (+) để thực hiện đăng ký app ID :

  3. Tick chọn App IDs và ấn nút Continue

  4. Nhập bundle ID và kéo xuống phần Push Notification để tick chọn và ấn nút Register

Bạn có thể xem bài viết : https://customersupport.doubledutch.me/hc/en-us/articles/229495568-iOS-How-to-Create-a-Push-Notification-Certificate tạo file certificate cho push notification:

2. Enable Push Notification Trong Firebase Cloud Messaging Console

Trên Firebase Console bạn sẽ đăng ký bundle id ở Xcode:

  • Trong màn hình quản lý Firebase bạn chọn Project Settings -> chọn tab Cloud Messaging :
  • Trong mục : APNs Certificates bạn update file .P12 lên:

Kết quả :

3. Viết code để tạo push notification.

import Firebase
import UIKit
import UserNotifications
import FirebaseFirestore class PushNotificationManager: NSObject, MessagingDelegate, UNUserNotificationCenterDelegate { let userID: String init(userID: String) { self.userID = userID super.init() } func registerForPushNotifications() { if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) // For iOS 10 data message (sent via FCM) Messaging.messaging().delegate = self } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) UIApplication.shared.registerUserNotificationSettings(settings) } UIApplication.shared.registerForRemoteNotifications() updateFirestorePushTokenIfNeeded() } func updateFirestorePushTokenIfNeeded() { if let token = Messaging.messaging().fcmToken { let usersRef = Firestore.firestore().collection("users_table").document(userID) usersRef.setData(["fcmToken": token], merge: true) } } func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { print(remoteMessage.appData) } func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { updateFirestorePushTokenIfNeeded() } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response) }
} 

Trong file AppDelegate.swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let pushManager = PushNotificationManager(userID: "currently_logged_in_user_id") pushManager.registerForPushNotifications() FirebaseApp.configure() return true } 

4. Send push notification từ Firebase Notifications dashboard

Trong Firebase Console. Ở menu bên trái chọn Grow -> Cloud Messaging. -> New Notification: Sau khi setting push bạn sẽ nhận được push :

Tài liệu tham khảo :

https://www.iosapptemplates.com/blog/ios-development/push-notifications-firebase-swift-5

Bình luận

Bài viết tương tự

- vừa được xem lúc

Học Flutter từ cơ bản đến nâng cao. Phần 1: Làm quen cô nàng Flutter

Lời mở đầu. Gần đây, Flutter nổi lên và được Google PR như một xu thế của lập trình di động vậy.

0 0 254

- vừa được xem lúc

Học Flutter từ cơ bản đến nâng cao. Phần 3: Lột trần cô nàng Flutter, BuildContext là gì?

Lời mở đầu. Màn làm quen cô nàng FLutter ở Phần 1 đã gieo rắc vào đầu chúng ta quá nhiều điều bí ẩn về nàng Flutter.

0 0 189

- vừa được xem lúc

Swift: Tạo custom phép toán tử (Operator) của riêng bạn!

Swift cho phép bạn tạo các toán tử có thể tùy chỉnh của riêng bạn. Điều này đặc biệt hữu ích khi bạn xử lý các loại dữ liệu của riêng mình. Operator Types in Swift. Có năm loại toán tử chính trong Swift.

0 0 45

- vừa được xem lúc

Chương 6 Protocol oriented programming.

Cuốn sách này là về lập trình hướng protocol. Khi Apple thông báo swift 2 ở WWDC 2015.

0 0 28

- vừa được xem lúc

Ví dụ về UIActivityViewController

Trên iOS, UIActivityViewController cung cấp giao diện thống nhất để người dùng chia sẻ và thực hiện các hành động trên văn bản, hình ảnh, URL và các mục khác trong ứng dụng. let string = "Hello, world!". let url = URL(string: "https://nshipster.com").

0 0 45

- vừa được xem lúc

Quản lý self và cancellable trong Combine.

. . Công việc quản lý bộ nhớ memory management thường trở nên phức tạp khi chúng ta thực hiện các tác vụ bất đồng bộ asynchronous vì chúng ta thường phải lưu giữ một số object nằm ngoài scope mà object được define trong khi vẫn phải đảm bảo được việc giải phóng object đó được thực hiện đúng quy trìn

0 0 28