| Chọn nền tảng: | iOS+ Android Web Flutter Unity C++ |
Sau khi được cài đặt trên thiết bị, ứng dụng khách có thể nhận thông báo thông qua giao diện APN FCM. Bạn có thể bắt đầu gửi ngay thông báo đến các phân khúc người dùng bằng Trình soạn thông báo trong bảng điều khiển Firebase hoặc tin nhắn được tạo trên máy chủ ứng dụng.
Xử lý thông báo cảnh báo
FCM phân phối tất cả thông báo nhắm đến các ứng dụng của Apple thông qua APN. Để tìm hiểu thêm về cách nhận thông báo APN bằng UNUserNotificationCenter, hãy xem tài liệu của Apple về Xử lý thông báo và các hành động liên quan đến thông báo.
Bạn phải đặt đại diện UNUserNotificationCenter và triển khai các phương thức đại diện thích hợp để nhận thông báo hiển thị từ FCM.
Swift
// Receive displayed notifications for iOS 10+ devices.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification) async
-> UNNotificationPresentationOptions {
let userInfo = notification.request.content.userInfo
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// ...
// Print full message.
print(userInfo)
// Change this to your preferred presentation option
// Note: UNNotificationPresentationOptions.alert has been deprecated.
return [.list, .banner, .sound]
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
let userInfo = response.notification.request.content.userInfo
// ...
// Print full message.
print(userInfo)
}
Objective-C
// Receive displayed notifications for iOS 10+ devices.
// Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Print full message.
NSLog(@"%@", userInfo);
// Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptionList |
UNNotificationPresentationOptionBanner |
UNNotificationPresentationOptionSound);
}
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
NSLog(@"FCM registration token: %@", fcmToken);
// Notify about received token.
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"FCMToken" object:nil userInfo:dataDict];
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
- (void)logFCMToken {
NSString *fcmToken = [FIRMessaging messaging].FCMToken;
NSLog(@"Local FCM registration token: %@", fcmToken);
NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];
[[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching the remote FCM registration token: %@", error);
} else {
NSLog(@"Remote FCM registration token: %@", token);
NSString* message =
[NSString stringWithFormat:@"FCM registration token: %@", token];
// display message
NSLog(@"%@", message);
}
}];
NSLog(@"%@", displayToken);
}
- (void)subsribeToTopic {
[[FIRMessaging messaging] subscribeToTopic:@"weather"
completion:^(NSError * _Nullable error) {
NSLog(@"Subscribed to weather topic");
}];
}
@end
Nếu bạn muốn thêm thao tác tuỳ chỉnh vào thông báo, hãy đặt tham số click_action trong tải trọng thông báo.
Sử dụng giá trị mà bạn sẽ dùng cho khoá category trong tải trọng APN.
Bạn phải đăng ký thao tác tuỳ chỉnh thì mới có thể sử dụng. Để biết thêm thông tin, hãy xem Hướng dẫn lập trình thông báo từ xa và thông báo cục bộ của Apple.
Để biết thông tin chi tiết về việc gửi thông báo đến ứng dụng, hãy chuyển đến trang tổng quan DevOps & Engagement (DevOps và mức độ tương tác) > Messaging (Nhắn tin) > Reports (Báo cáo) trong bảng điều khiển Firebase. Trang tổng quan này ghi lại số lượng tin nhắn được gửi và mở trên thiết bị Apple và thiết bị Android, cùng với dữ liệu về "số lượt hiển thị" (thông báo mà người dùng nhìn thấy) cho ứng dụng Android.
Xử lý thông báo đẩy im lặng
Khi gửi tin nhắn bằng khoá content-available (tương đương với content-available của APN), tin nhắn sẽ được gửi dưới dạng thông báo im lặng, đánh thức ứng dụng của bạn ở chế độ nền để thực hiện các tác vụ như làm mới dữ liệu nền.
Không giống như thông báo trên nền trước, bạn phải xử lý các thông báo này bằng phương thức application(_:didReceiveRemoteNotification:fetchCompletionHandler:).
Triển khai application(_:didReceiveRemoteNotification:fetchCompletionHandler:) như sau:
Swift
@MainActor
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async
-> UIBackgroundFetchResult {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// Messaging.messaging().appDidReceiveMessage(userInfo)
// ...
// Print full message.
print(userInfo)
print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
return UIBackgroundFetchResult.newData
}
Objective-C
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired until the user taps on the notification launching the application.
// TODO: Handle data of notification
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Print full message.
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
Nền tảng của Apple không đảm bảo việc gửi thông báo ở chế độ nền. Để tìm hiểu về các điều kiện có thể khiến thông báo ở chế độ nền không thành công, hãy xem tài liệu của Apple về Đẩy bản cập nhật ở chế độ nền cho ứng dụng.
Diễn giải tải trọng thông báo
Nội dung của thông báo là một từ điển gồm các khoá và giá trị. Thông báo được gửi qua APN có định dạng tải trọng APN như sau:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
Xử lý thông báo khi tắt tính năng thay đổi phương thức
Theo mặc định, nếu bạn chỉ định lớp uỷ quyền ứng dụng của ứng dụng cho các thuộc tính uỷ quyền UNUserNotificationCenter và Messaging, thì FCM sẽ hoán đổi lớp uỷ quyền ứng dụng để tự động liên kết mã thông báo FCM với mã thông báo APN của thiết bị và truyền các sự kiện đã nhận thông báo đến Analytics. Nếu bạn tắt tính năng thay đổi phương thức một cách rõ ràng, nếu bạn đang xây dựng một ứng dụng SwiftUI hoặc nếu bạn sử dụng một lớp riêng biệt cho một trong hai uỷ quyền, thì bạn sẽ cần phải thực hiện cả hai nhiệm vụ này theo cách thủ công.
Để liên kết mã thông báo FCM với mã thông báo APN của thiết bị, hãy truyền mã thông báo APN đến lớp Messaging trong trình xử lý làm mới mã thông báo của trình uỷ nhiệm ứng dụng bằng cách sử dụng thuộc tính apnsToken.
Swift
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken;
}
Objective-C
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
}
Để chuyển thông tin biên nhận thông báo đến Analytics, hãy sử dụng phương thức appDidReceiveMessage(_:).
Swift
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler()
}
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Messaging.messaging().appDidReceiveMessage(userInfo)
completionHandler(.noData)
}
Objective-C
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo;
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
completionHandler();
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];
completionHandler(UIBackgroundFetchResultNoData);
}