| اختيار المنصة: | iOS+ Android Web Flutter Unity C++ |
بعد تثبيت تطبيق العميل على جهاز، يمكنه تلقّي الرسائل من خلال واجهة برمجة تطبيقات APNs FCM. يمكنك البدء على الفور بإرسال إشعارات إلى شرائح المستخدمين باستخدام أداة إنشاء الإشعارات في Firebase Console، أو الرسائل التي تم إنشاؤها على خادم تطبيقك.
التعامل مع إشعارات التنبيه
FCM يرسل جميع الرسائل التي تستهدف تطبيقات Apple من خلال APNs. لمزيد من المعلومات حول تلقّي إشعارات APNs باستخدام 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 في حمولة APNs.
يجب تسجيل الإجراءات المخصّصة قبل أن تتمكّن من استخدامها. لمزيد من
المعلومات، اطّلِع على دليل برمجة الإشعارات المحلية والإشعارات عن بُعد من Apple
.
للاطّلاع على معلومات حول تسليم الرسائل إلى تطبيقك، انتقِل إلى لوحة بيانات DevOps والتفاعل > المراسلة > التقارير في Firebase Console. تسجِّل لوحة البيانات هذه عدد الرسائل المرسَلة والمفتوحة على أجهزة Apple وAndroid، بالإضافة إلى بيانات "مرات الظهور" (الإشعارات التي يراها المستخدمون) لتطبيقات Android.
التعامل مع الإشعارات الفورية الصامتة
عند إرسال رسائل باستخدام المفتاح content-available (المكافئ للمفتاح content-available في APNs)، سيتم تسليم الرسائل كإشعارات صامتة، ما يؤدي إلى تنبيه تطبيقك في الخلفية لإجراء مهام مثل تحديث بيانات الخلفية.
على عكس الإشعارات التي تظهر في المقدّمة، يجب التعامل مع هذه الإشعارات باستخدام الـ
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 حولإرسال التعديلات في الخلفية إلى تطبيقك.
تفسير حمولة رسالة الإشعار
حمولة رسائل الإشعارات هي قاموس من المفاتيح والقيم. تتّبع رسائل الإشعارات المرسَلة من خلال APNs تنسيق حمولة APNs التالي:
{
"aps" : {
"alert" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
},
"badge" : 1,
},
"customKey" : "customValue"
}
التعامل مع الرسائل مع إيقاف ميزة تبديل الطرق
بشكلٍ تلقائي، إذا عيّنت فئة مفوَّض التطبيق لتطبيقك لخصائص المفوَّض
UNUserNotificationCenter وMessaging، سيقوم FCM
بتبديل فئة مفوَّض التطبيق لربط رمز
FCM تلقائيًا برمز APNs للجهاز وتمرير الأحداث التي تشير إلى تلقّي الإشعارات إلى Analytics. إذا أوقفت ميزة تغيير وظيفة الإجراء بشكلٍ صريح، أو إذا كنت تنشئ تطبيق SwiftUI، أو إذا كنت تستخدم فئة منفصلة لأي من المفوَّضَين، عليك تنفيذ هاتَين المهمتَين يدويًا.
لربط الرمز FCM برمز APNs للجهاز، مرِّر رمز APNs
إلى فئة 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);
}