این راهنما نحوه تنظیم Firebase Cloud Messaging را در برنامههای موبایل و کلاینت وب شما شرح میدهد تا بتوانید پیامها را به طور قابل اعتمادی دریافت کنید.
پس از نصب برنامه کلاینت شما روی یک دستگاه، میتواند از طریق رابط FCM APN پیامها را دریافت کند. میتوانید بلافاصله با استفاده از آهنگساز Notifications یا پیامهای ساخته شده روی سرور برنامه خود، ارسال اعلانها به بخشهای کاربر را شروع کنید.
مدیریت اعلانهای هشدار
FCM تمام پیامهایی را که برنامههای اپل را هدف قرار میدهند از طریق APNها ارسال میکند. برای کسب اطلاعات بیشتر در مورد دریافت اعلانهای APN با استفاده از UNUserNotificationCenter
، به مستندات اپل در مورد مدیریت اعلانها و اقدامات مرتبط با اعلان مراجعه کنید.
شما باید نماینده UNUserNotificationCenter را تنظیم کرده و متدهای نماینده مناسب را برای دریافت اعلانهای نمایشی از FCM پیادهسازی کنید.
سویفت
extension AppDelegate: UNUserNotificationCenterDelegate {
// 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.
if #available(iOS 14.0, *) {
return [.list, .banner, .sound]
} else {
return [.alert, .sound]
}
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse) async {
let userInfo = response.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)
}
}
هدف-سی
// 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
// Note: UNNotificationPresentationOptionAlert has been deprecated.
if (@available(iOS 14.0, *)) {
completionHandler(UNNotificationPresentationOptionList |
UNNotificationPresentationOptionBanner |
UNNotificationPresentationOptionSound);
} else {
completionHandler(UNNotificationPresentationOptionAlert |
UNNotificationPresentationOptionSound);
}
}
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void(^)(void))completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
}
// With swizzling disabled you must let Messaging know about the message, for Analytics
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// Print full message.
NSLog(@"%@", userInfo);
completionHandler();
}
اگر میخواهید اقدامات سفارشی به اعلانهای خود اضافه کنید، پارامتر click_action
را در payload اعلان تنظیم کنید. از مقداری که برای کلید category
در payload APNs استفاده میکنید، استفاده کنید. اقدامات سفارشی قبل از استفاده باید ثبت شوند. برای اطلاعات بیشتر، به راهنمای برنامهنویسی اعلانهای محلی و از راه دور اپل مراجعه کنید.
برای اطلاع از نحوهی ارسال پیام به برنامهی خود، به داشبورد گزارشدهی FCM مراجعه کنید که تعداد پیامهای ارسالی و باز شده در دستگاههای اپل و اندروید را به همراه دادههای مربوط به «نمایشها» (اعلانهای مشاهده شده توسط کاربران) برای برنامههای اندروید ثبت میکند.
اعلانهای بیصدا را مدیریت کنید
هنگام ارسال پیام با کلید content-available
(معادل content-available
در APNها)، پیامها به صورت اعلانهای بیصدا ارسال میشوند و برنامه شما را در پسزمینه برای کارهایی مانند بهروزرسانی دادههای پسزمینه فعال میکنند. برخلاف اعلانهای پیشزمینه، این اعلانها باید با استفاده از متد application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
مدیریت شوند.
پیادهسازی application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
به صورت نشان داده شده:
سویفت
@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 message ID.
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
// Print full message.
print(userInfo)
print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
return UIBackgroundFetchResult.newData
}
هدف-سی
- (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 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
// [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
// ...
// Print full message.
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
پلتفرمهای اپل تحویل اعلانهای پسزمینه را تضمین نمیکنند. برای کسب اطلاعات بیشتر در مورد شرایطی که میتواند باعث از کار افتادن اعلانهای پسزمینه شود، به اسناد اپل در مورد «ارسال بهروزرسانیهای پسزمینه به برنامه شما» مراجعه کنید.
تفسیر محتوای پیام اعلان
محتوای پیامهای اعلان، یک دیکشنری از کلیدها و مقادیر است. پیامهای اعلان ارسالی از طریق APNها، قالب محتوای APN زیر را دارند:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
مدیریت پیامها با غیرفعال بودن متد swizzling
By default, if you assign your app's app delegate class to the UNUserNotificationCenter
and Messaging
delegate properties, FCM will swizzle your app delegate class to automatically associate your FCM token with the device's APNs token and pass notification-received events to Analytics . If you explicitly disable method swizzling, if you are building a SwiftUI app, or if you use a separate class for either delegate, you will need to perform both of these tasks manually.
برای مرتبط کردن توکن FCM با توکن APN دستگاه، توکن APN را با استفاده از ویژگی apnsToken
به کلاس Messaging
در کنترلکنندهی بهروزرسانی توکن نمایندهی برنامهی خود منتقل کنید.
سویفت
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken;
}
هدف-سی
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
}
برای ارسال اطلاعات رسید اعلان به Analytics ، از متد appDidReceiveMessage(_:)
استفاده کنید.
سویفت
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)
}
هدف-سی
- (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);
}