| انتخاب پلاتفرم: | iOS+ Android وب Flutter Unity C++ |
وقتی برنامه مشتری در دستگاهی نصب شد، میتواند ازطریق میانای APN FCM پیام دریافت کند. میتوانید بلافاصله ارسال اعلانها به بخشهای کاربر را با سازنده اعلانها در کنسول Firebase یا پیامهای ساختهشده در سرور برنامهتان شروع کنید.
مدیریت اعلانهای هشدار
FCM همه پیامهایی را که برنامههای Apple را هدف قرار میدهند ازطریق APN ارسال میکند. برای کسب اطلاعات بیشتر درباره دریافت اعلانهای APN بااستفاده از UNUserNotificationCenter، به اسناد Apple درباره مدیریت اعلانها و کنشهای مرتبط با اعلان مراجعه کنید.
باید نماینده UNUserNotificationCenter را تنظیم کنید و روشهای نماینده مناسب را برای دریافت اعلانهای نمایشی از 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
اگر میخواهید کنشهای سفارشی به اعلانهایتان اضافه کنید، پارامتر click_action را در بار داده اعلان تنظیم کنید.
از مقداری استفاده کنید که برای کلید category در بار APN استفاده میکنید.
کنشهای سفارشی باید قبلاز استفاده ثبت شوند. برای اطلاعات بیشتر، به
راهنمای برنامهنویسی اعلان محلی و ازراهدور Apple مراجعه کنید.
برای دریافت اطلاعات آماری درباره تحویل پیام به برنامهتان، به داشبورد DevOps و تعامل > پیامرسانی > گزارشها در کنسول Firebase بروید. این داشبورد تعداد پیامهای ارسالشده و بازشده در دستگاههای Apple و Android را بههمراه دادههای مربوط به «ظهورها» (اعلانهای دیدهشده توسط کاربران) برای برنامههای Android ثبت میکند.
مدیریت اعلانهای لحظهای بیصدا
هنگام ارسال پیام با کلید content-available (معادل
content-available در APN)، پیامها بهعنوان اعلانهای بیصدا ارسال میشوند
و برنامهتان را در پسزمینه برای کارهایی مثل بازآوری دادههای پسزمینه فعال میکنند.
برخلاف اعلانهای پیشزمینه، این اعلانها باید بااستفاده از روش
application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
مدیریت شوند.
application(_:didReceiveRemoteNotification:fetchCompletionHandler:) را
بهصورت نشاندادهشده پیادهسازی کنید:
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);
}
پلاتفرمهای Apple تحویل اعلانهای پسزمینه را تضمین نمیکنند. برای آشنایی با شرایطی که میتواند باعث عدم موفقیت اعلانهای پسزمینه شود، به اسناد Apple درباره ارسال بهروزرسانیهای پسزمینه به برنامه مراجعه کنید.
تفسیر کردن محموله پیام اعلان
بار پیامهای اعلان واژهنامهای از کلیدها و مقادیر است. پیامهای اعلانی که ازطریق APN ارسال میشوند، قالب بار APN زیر را دارند:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
رسیدگی به پیامها با غیرفعال کردن روش تغییر پیادهسازی
بهطور پیشفرض، اگر کلاس نماینده برنامه خود را به ویژگیهای نماینده
UNUserNotificationCenter و Messaging اختصاص دهید، FCM
کلاس نماینده برنامه شما را تغییر میدهد تا بهطور خودکار
ژتون FCM شما را با ژتون APN دستگاه مرتبط کند و رویدادهای دریافت اعلان را به Analytics منتقل کند. اگر روش تغییر پیادهسازی را بهطور صریح غیرفعال کنید، اگر درحال ساختن برنامه SwiftUI هستید، یا اگر از کلاس جداگانهای برای هریک از نمایندگان استفاده میکنید، باید هر دو این کارها را بهصورت دستی انجام دهید.
برای مرتبط کردن کد FCM با کد APN دستگاه، کد APN را به کلاس Messaging در
گرداننده بازآوری کد
نماینده برنامهتان بااستفاده از
ویژگی 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;
}
برای ارسال اطلاعات رسید اعلان به Analytics، از
روش 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);
}