รับข้อความในแอป Apple

เมื่อติดตั้งแอปไคลเอ็นต์ในอุปกรณ์แล้ว แอปจะรับข้อความได้ผ่าน อินเทอร์เฟซ APNs ของ FCM คุณสามารถเริ่มใช้งานได้ทันที ส่งการแจ้งเตือนไปยังกลุ่มผู้ใช้ด้วย การเขียนการแจ้งเตือน หรือข้อความที่สร้างขึ้นจาก แอปพลิเคชันเซิร์ฟเวอร์

จัดการการแจ้งเตือน

FCM ส่งข้อความทั้งหมดที่กำหนดเป้าหมายไปยังแอป Apple ผ่าน APN เพื่อดูข้อมูลเพิ่มเติม เกี่ยวกับการรับการแจ้งเตือน APN ผ่านทาง UNUserNotificationCenter โปรดดู เอกสารประกอบใน การจัดการการแจ้งเตือนและการดำเนินการที่เกี่ยวข้องกับการแจ้งเตือน

คุณต้องตั้งค่า ผู้รับมอบสิทธิ์ UNUserNotificationCenter และใช้วิธีการผู้รับมอบสิทธิ์ที่เหมาะสมในการรับการแจ้งเตือน จาก FCM

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 ในเพย์โหลด APNs ต้องลงทะเบียนการดำเนินการที่กำหนดเองก่อน จะสามารถใช้ได้ สำหรับข้อมูลเพิ่มเติม โปรดดู คู่มือการจัดโปรแกรมการแจ้งเตือนสำหรับท้องถิ่นและระยะไกล

ดูข้อมูลเชิงลึกเกี่ยวกับการส่งข้อความไปที่แอปได้ที่ แดชบอร์ดการรายงาน FCM ซึ่งบันทึก จำนวนข้อความที่ส่งและเปิดบนอุปกรณ์ Apple และ Android พร้อมกับ ข้อมูลสำหรับ "การแสดงผล" (การแจ้งเตือนที่ผู้ใช้เห็น) สำหรับแอป Android

จัดการข้อความ Push แบบไม่มีเสียง

เมื่อส่งข้อความด้วยคีย์ content-available (เทียบเท่ากับของ APNs 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 รายการ, FCM จะแสดงชั้นเรียนที่มอบสิทธิ์ของแอปเพื่อเชื่อมโยง โทเค็น FCM พร้อมโทเค็น APN ของอุปกรณ์และการแจ้งเตือนส่งผ่าน เหตุการณ์ลงใน Analytics หากคุณปิดใช้ Method ของ Swirl อย่างชัดเจน หากคุณ ในการสร้างแอป SwiftUI หรือหากคุณใช้คลาสแยกต่างหากสำหรับผู้รับมอบสิทธิ์อย่างใดอย่างหนึ่ง คุณจะ จะต้องดำเนินการทั้ง 2 อย่างนี้ด้วยตนเอง

หากต้องการเชื่อมโยงโทเค็น FCM กับโทเค็น APN ของอุปกรณ์ ให้ส่ง 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;
}

ในการส่งข้อมูลใบเสร็จการแจ้งเตือนไปยัง Analytics ให้ใช้ 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);
}