استلام الرسائل في تطبيق Apple

بعد تثبيت تطبيق العميل على أحد الأجهزة، يمكنه تلقّي الرسائل من خلال واجهة FCM APNs. يمكنك البدء على الفور في إرسال الإشعارات إلى شرائح المستخدمين باستخدام أداة إنشاء الإشعارات، أو الرسائل التي تم إنشاؤها على خادم التطبيق.

التعامل مع إشعارات التنبيه

تُرسِل FCM جميع الرسائل التي تستهدف تطبيقات Apple من خلال APNs. لمزيد من المعلومات حول تلقّي إشعارات APNs من خلال UNUserNotificationCenter، راجِع مستندات Apple حول التعامل مع الإشعارات والإجراءات ذات الصلة بالإشعارات.

يجب ضبط مفوّض UNUserNotificationCenter وتنفيذ طرق التفويض المناسبة لتلقّي إشعارات العرض من FCM.

Swift

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)
  }
}

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(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 في حمولة APNs. يجب تسجيل الإجراءات المخصّصة قبل استخدامها. لمزيد من المعلومات، يُرجى الاطّلاع على دليل برمجة الإشعارات المحلية والبعيدة من Apple.

للحصول على إحصاءات حول تسليم الرسائل إلى تطبيقك، اطّلِع على FCMلوحة بيانات التقارير التي تسجّل عدد الرسائل المرسَلة والمفتوحة على أجهزة Apple وAndroid، بالإضافة إلى بيانات "مرات الظهور" (الإشعارات التي يراها المستخدمون) لتطبيقات Android.

التعامل مع الإشعارات الفورية الصامتة

عند إرسال الرسائل باستخدام مفتاح content-available (المكافئ لمفتاح content-available في APNs)، سيتم تسليم الرسائل كإشعارات صامتة، ما يؤدي إلى تنشيط تطبيقك في الخلفية لتنفيذ مهام مثل إعادة تحميل البيانات في الخلفية. وعلى عكس الإشعارات التي تظهر في المقدّمة، يجب التعامل مع هذه الإشعارات من خلال الطريقة application(_:didReceiveRemoteNotification:fetchCompletionHandler:).

نفِّذ application(_:didReceiveRemoteNotification:fetchCompletionHandler:) كما هو موضّح:

Swift

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
}

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 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);
}

لا تضمن منصات 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);
}