קבלת הודעות באפליקציית אפל

ברגע שהאפליקציה של הלקוח שלך מותקנת במכשיר, היא יכולה לקבל הודעות דרך ממשק ה-APNs של FCM. אתה יכול להתחיל לשלוח הודעות מיידית למקטעי משתמשים באמצעות ה- Notifications composer , או הודעות שנבנו בשרת היישומים שלך.

טיפול בהודעות התראה

FCM מספקת את כל ההודעות המכוונות לאפליקציות אפל דרך 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)
  }
}

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. יש לרשום פעולות מותאמות אישית לפני שניתן יהיה להשתמש בהן. למידע נוסף, עיין במדריך תכנות הודעות מקומיות ומרוחקות של אפל.

לקבלת תובנות לגבי מסירת הודעות לאפליקציה שלך, עיין בלוח המחוונים לדיווח של FCM , שמתעד את מספר ההודעות שנשלחו ונפתחו במכשירי אפל ו-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
}

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

פלטפורמות אפל אינן מבטיחות מסירת הודעות רקע. כדי ללמוד על תנאים שעלולים לגרום להתראות רקע להיכשל, עיין במסמכים של אפל בנושא דחיפת עדכוני רקע לאפליקציה שלך .

פירוש מטען הודעת הודעה

המטען של הודעות ההתראה הוא מילון של מפתחות וערכים. הודעות הודעה שנשלחות דרך APN עוקבות אחר פורמט המטען של APNs כמפורט להלן:

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

טפל בהודעות עם שיטת swizzling מושבתת

כברירת מחדל, אם תקצה את מחלקת נציג האפליקציה של האפליקציה שלך לנכסי נציג האפליקציה UNUserNotificationCenter ו- Messaging , FCM תעביר את מחלקת נציג האפליקציה שלך כדי לשייך אוטומטית את אסימון ה-FCM שלך לאסימון ה-APN של המכשיר ולהעביר אירועים שהתקבלו בהתראות ל-Analytics. אם אתה משבית במפורש את שיטת swizzling, אם אתה בונה אפליקציית SwiftUI, או אם אתה משתמש במחלקה נפרדת עבור כל אחד מהנציגים, תצטרך לבצע את שתי המשימות הללו באופן ידני.

כדי לשייך את אסימון ה-FCM עם אסימון ה-APN של המכשיר, העבר את אסימון ה-APNs למחלקת Messaging במטפל לרענון האסימון של נציג האפליקציה שלך דרך המאפיין apnsToken .

מָהִיר

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(_:) .

מָהִיר

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