این راهنما نحوه تنظیم Firebase Cloud Messaging در برنامههای موبایل و کلاینت وب شما شرح میدهد تا بتوانید پیامها را به طور قابل اعتمادی دریافت کنید.
پس از نصب برنامه کلاینت شما روی یک دستگاه، میتواند از طریق رابط FCM APN پیامها را دریافت کند. میتوانید بلافاصله با استفاده از آهنگساز Notifications یا پیامهای ساخته شده روی سرور برنامه خود، ارسال اعلانها به بخشهای کاربر را شروع کنید.
مدیریت اعلانهای هشدار
FCM تمام پیامهایی را که برنامههای اپل را هدف قرار میدهند از طریق APNها ارسال میکند. برای کسب اطلاعات بیشتر در مورد دریافت اعلانهای APN با استفاده از UNUserNotificationCenter ، به مستندات اپل در مورد مدیریت اعلانها و اقدامات مرتبط با اعلان مراجعه کنید.
شما باید نماینده UNUserNotificationCenter را تنظیم کرده و متدهای نماینده مناسب را برای دریافت اعلانهای نمایشی از FCM پیادهسازی کنید.
سویفت
// 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)
}
هدف-سی
// 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
اگر میخواهید اقدامات سفارشی به اعلانهای خود اضافه کنید، پارامتر 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 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 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);
}
پلتفرمهای اپل تحویل اعلانهای پسزمینه را تضمین نمیکنند. برای کسب اطلاعات بیشتر در مورد شرایطی که میتواند باعث از کار افتادن اعلانهای پسزمینه شود، به اسناد اپل در مورد «ارسال بهروزرسانیهای پسزمینه به برنامه شما» مراجعه کنید.
تفسیر محتوای پیام اعلان
محتوای پیامهای اعلان، یک دیکشنری از کلیدها و مقادیر است. پیامهای اعلان ارسالی از طریق APNها، قالب محتوای APN زیر را دارند:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
مدیریت پیامها با غیرفعال بودن متد swizzling
به طور پیشفرض، اگر کلاس نماینده برنامه خود را به ویژگیهای نماینده UNUserNotificationCenter و Messaging اختصاص دهید، FCM کلاس نماینده برنامه شما را swizzle میکند تا به طور خودکار توکن FCM شما را با توکن APN های دستگاه مرتبط کند و رویدادهای دریافتی اعلان را به Analytics منتقل کند. اگر صریحاً متد swizzling را غیرفعال کنید، اگر در حال ساخت یک برنامه SwiftUI هستید، یا اگر از یک کلاس جداگانه برای هر یک از نمایندهها استفاده میکنید، باید هر دوی این وظایف را به صورت دستی انجام دهید.
برای مرتبط کردن توکن 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);
}