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

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

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

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

التعامل مع الرسائل مع تعطيل الطريقة

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

لربط رمز FCM المميز برمز APN الخاص بالجهاز، قم بتمرير رمز 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);
}