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

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

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

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

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

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

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

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

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

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

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

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

إذا خصَّصت تلقائيًا فئة تفويض تطبيقك في تطبيقك للمواقع الإلكترونية المفوَّضة UNUserNotificationCenter وMessaging، ستعمل ميزة "المراسلة عبر السحابة الإلكترونية من Firebase" على تغيير تفويض صف تطبيقك لربطه تلقائيًا بالرمز المميّز APNs للجهاز، ثم نقل الأحداث التي يتم تلقّي الإشعارات إليها إلى "إحصاءات 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);
}