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

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

التعامل مع إخطارات التنبيه

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

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

سويفت


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

ج موضوعية

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

تطبيق application(_:didReceiveRemoteNotification:fetchCompletionHandler:) كما هو موضح:

سويفت

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
}

ج موضوعية

- (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"
  }

التعامل مع الرسائل مع تعطيل أسلوب swizzling

بشكل افتراضي ، إذا قمت بتعيين فئة مفوض التطبيق للتطبيق الخاص بك إلى UNUserNotificationCenter وخصائص مندوب Messaging ، فسيقوم FCM بتبديل فئة مفوض التطبيق الخاص بك لربط رمز FCM المميز الخاص بك تلقائيًا برمز APNs الخاص بالجهاز وتمرير الأحداث المستلمة بالإشعارات إلى Analytics. إذا قمت بتعطيل طريقة swizzling بشكل صريح ، إذا كنت تقوم بإنشاء تطبيق SwiftUI ، أو إذا كنت تستخدم فئة منفصلة لأي من المفوضين ، فستحتاج إلى تنفيذ هاتين المهمتين يدويًا.

لربط رمز FCM المميز برمز APNs الخاص بالجهاز ، قم بتمرير رمز APNs المميز إلى فئة Messaging في معالج تحديث الرمز المميز لمفوض التطبيق عبر خاصية apnsToken .

سويفت

func application(_ application: UIApplication,
    didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  Messaging.messaging().apnsToken = deviceToken;
}
 

ج موضوعية

- (void)application:(UIApplication *)application
    didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [FIRMessaging messaging].APNSToken = deviceToken;
}

لتمرير معلومات استلام الإشعارات إلى Analytics ، استخدم طريقة appDidReceiveMessage(_:) .

سويفت

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

ج موضوعية

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