این راهنما نحوه راهاندازی Firebase Cloud Messaging در برنامههای موبایل و کلاینت وب خود توضیح میدهد تا بتوانید پیامها را بهطور مطمئن دریافت کنید.
هنگامی که برنامه مشتری شما روی دستگاهی نصب شد، می تواند پیام ها را از طریق رابط FCM APNs دریافت کند. میتوانید بلافاصله اعلانها را به بخشهای کاربر با آهنگساز اعلانها یا پیامهای ساخته شده روی سرور برنامهتان ارسال کنید.
اعلانهای هشدار را مدیریت کنید
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
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)
}
}
هدف-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(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionAlert);
}
// 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
را در بارگذاری اعلان تنظیم کنید. از مقداری که برای کلید category
در بارگذاری APN استفاده می کنید استفاده کنید. اقدامات سفارشی قبل از استفاده باید ثبت شوند. برای اطلاعات بیشتر، به راهنمای برنامهنویسی اعلانهای محلی و راه دور اپل مراجعه کنید.
برای اطلاعات بیشتر در مورد تحویل پیام به برنامه خود، به داشبورد گزارش FCM مراجعه کنید، که تعداد پیامهای ارسال شده و باز شده در دستگاههای Apple و Android را به همراه دادههای «impressions» (اعلانهایی که کاربران مشاهده میکنند) را برای برنامههای Android ثبت میکند.
اعلانهای فشار بیصدا را مدیریت کنید
هنگام ارسال پیامها با کلید content-available
(معادل content-available
در APN)، پیامها بهعنوان اعلانهای بیصدا تحویل داده میشوند و برنامه شما را در پسزمینه برای کارهایی مانند بازخوانی دادههای پسزمینه بیدار میکند. برخلاف اعلانهای پیشزمینه، این اعلانها باید با استفاده از روش application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
مدیریت شوند.
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
مطابق شکل پیاده کنید:
سویفت
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)
return UIBackgroundFetchResult.newData
}
هدف-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 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"
}
مدیریت پیامها با تغییر روش غیرفعال
بهطور پیشفرض، اگر کلاس نماینده برنامه برنامهتان را به ویژگیهای UNUserNotificationCenter
و Messaging
اختصاص دهید، FCM کلاس نماینده برنامه شما را تغییر میدهد تا رمز FCM شما بهطور خودکار با توکن APN دستگاه مرتبط شود و رویدادهای دریافت اعلانها را به Analytics ارسال کند. اگر به صراحت روش Swizzling را غیرفعال کنید، اگر در حال ساخت یک برنامه SwiftUI هستید، یا اگر از یک کلاس جداگانه برای هر یک از نمایندگان استفاده می کنید، باید هر دوی این کارها را به صورت دستی انجام دهید.
برای مرتبط کردن نشانه FCM با توکن APNs دستگاه، با استفاده از ویژگی apnsToken
، توکن APNs را به کلاس Messaging
در کنترلکننده تازهسازی رمز نماینده برنامه خود منتقل کنید.
سویفت
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken;
}
هدف-C
- (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)
}
هدف-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);
}