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

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

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

تعرض خدمة "المراسلة عبر السحابة الإلكترونية من Firebase" جميع الرسائل التي تستهدف تطبيقات Apple من خلال أسماء نقاط الوصول (APN). معرفة المزيد حول تلقّي إشعارات أسماء نقاط الوصول (APN) عبر UNUserNotificationCenter، راجع وثائق حول التعامل مع الإشعارات والإجراءات المتعلقة بالإشعارات:

يجب عليك تعيين تفويض UNUserNotificationCenter وتنفيذ أساليب التفويض المناسبة لتلقي إشعارات العرض من خدمة "المراسلة عبر السحابة الإلكترونية من Firebase"

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

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

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

عند إرسال رسائل باستخدام مفتاح content-available (بما في ذلك مفتاح أسماء نقاط الوصول (APN) content-available، سيتم تسليم الرسائل كإشعارات صامتة. تشغيل تطبيقك في الخلفية لمهام مثل إعادة تحميل بيانات الخلفية. على عكس الإشعارات التي تعمل في المقدّمة، يجب التعامل معها من خلال 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 على إرسال تحديثات تطبيقك في الخلفية:

تفسير حمولة رسائل الإشعار

تُعد حمولة رسائل الإشعارات قاموسًا والقيم. رسائل الإشعارات المُرسَلة من خلال أسماء نقاط الوصول (APN) تتبع أسماء نقاط الوصول (APN) تنسيق حمولة البيانات على النحو التالي:

  {
    "aps" : {
      "alert" : {
        "body" : "great match!",
        "title" : "Portugal vs. Denmark",
      },
      "badge" : 1,
    },
    "customKey" : "customValue"
  }

التعامل مع الرسائل عندما تكون طريقة التبديل غير مفعّلة

بشكل تلقائي، إذا عيّنت فئة تفويض التطبيق في تطبيقك إلى UNUserNotificationCenter وMessaging موقع إلكتروني المفوَّض، ميزة "المراسلة عبر السحابة الإلكترونية من Firebase" سيؤدي إلى ربط صف تفويض تطبيقك لربط رمز "المراسلة عبر السحابة الإلكترونية من Firebase" مع رمز نقاط الوصول (APN) للجهاز وإشعارات البطاقة التي تم تلقّيها الأحداث إلى "إحصاءات Google" إذا عطلت طريقة swizzling بشكل صريح، إذا كنت تنشئ تطبيق SwiftUI أو إذا كنت تستخدم فئةً منفصلة لأيٍ من المفوَّضين، يمكنك إلى أداء هاتين المهمتين يدويًا.

لربط الرمز المميّز لخدمة "المراسلة عبر السحابة الإلكترونية من Firebase" بالرمز المميّز لأسماء نقاط الوصول (APNs) للأجهزة، يجب ضبط أسماء نقاط الوصول (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;
}

لإرسال معلومات إيصال الإشعارات إلى "إحصاءات Google"، استخدِم طريقة 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);
}