รับข้อความในแอปแพลตฟอร์ม Apple

เลือกแพลตฟอร์ม: iOS+ Android Web Flutter Unity C++


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

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

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

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

Swift

// 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
  // Note: UNNotificationPresentationOptions.alert has been deprecated.
  return [.list, .banner, .sound]
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse) async {
  let userInfo = response.notification.request.content.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(UNNotificationPresentationOptionList |
                    UNNotificationPresentationOptionBanner |
                    UNNotificationPresentationOptionSound);
}

- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
    NSLog(@"FCM registration token: %@", fcmToken);
    // Notify about received token.
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
    [[NSNotificationCenter defaultCenter] postNotificationName:
     @"FCMToken" object:nil userInfo:dataDict];
    // TODO: If necessary send token to application server.
    // Note: This callback is fired at each app startup and whenever a new token is generated.
}

- (void)logFCMToken {
  NSString *fcmToken = [FIRMessaging messaging].FCMToken;
  NSLog(@"Local FCM registration token: %@", fcmToken);

  NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];

  [[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
    if (error != nil) {
      NSLog(@"Error fetching the remote FCM registration token: %@", error);
    } else {
      NSLog(@"Remote FCM registration token: %@", token);
      NSString* message =
        [NSString stringWithFormat:@"FCM registration token: %@", token];
      // display message
      NSLog(@"%@", message);
    }
  }];
  NSLog(@"%@", displayToken);
}

- (void)subsribeToTopic {
  [[FIRMessaging messaging] subscribeToTopic:@"weather"
             ompletion:^(NSError * _Nullable error) {
    NSLog(@"Subscribed to weather topic");
  }];
}

@end
ObjCMigratedSnippets.m

หากต้องการเพิ่มการดำเนินการที่กำหนดเองในการแจ้งเตือน ให้ตั้งค่าclick_action พารามิเตอร์ใน เพย์โหลดการแจ้งเตือน ใช้ค่าที่คุณจะใช้สำหรับคีย์ category ในเพย์โหลด APNs คุณต้องลงทะเบียนการดำเนินการที่กำหนดเองก่อนจึงจะใช้งานได้ ดูข้อมูลเพิ่มเติมได้ใน คู่มือการเขียนโปรแกรมการแจ้งเตือนในเครื่องและระยะไกลของ Apple

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

จัดการการแจ้งเตือนแบบ Push ที่ไม่มีเสียง

เมื่อส่งข้อความที่มีคีย์ content-available (เทียบเท่ากับ content-available ของ APNs) ระบบจะส่งข้อความเป็นการแจ้งเตือนที่ไม่มีเสียง ซึ่งจะปลุกแอปในเบื้องหลังเพื่อทำงานต่างๆ เช่น การรีเฟรชข้อมูลในเบื้องหลัง การแจ้งเตือนเหล่านี้ต้องได้รับการจัดการโดยใช้ application(_:didReceiveRemoteNotification:fetchCompletionHandler:) เมธอด ซึ่งแตกต่างจากการแจ้งเตือนเบื้องหน้า

ใช้ application(_:didReceiveRemoteNotification:fetchCompletionHandler:) ดังที่แสดงด้านล่าง

Swift

@MainActor
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 full message.
  print(userInfo)
  print("Call exportDeliveryMetricsToBigQuery() from AppDelegate")
  Messaging.serviceExtension().exportDeliveryMetricsToBigQuery(withMessageInfo: userInfo)
  return UIBackgroundFetchResultpets.swift

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 until 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(UIBackgroundFetchResultNSnippets.m

แพลตฟอร์ม Apple ไม่รับประกันการส่งการแจ้งเตือนเบื้องหลัง ดูข้อมูลเกี่ยวกับเงื่อนไขที่อาจทำให้การแจ้งเตือนเบื้องหลังไม่สำเร็จได้ใน เอกสารประกอบของ Apple เกี่ยวกับ การส่งการอัปเดตเบื้องหลังไปยังแอป

ตีความเพย์โหลดข้อความแจ้งเตือน

เพย์โหลดของข้อความแจ้งเตือนคือพจนานุกรมของคีย์และค่า ข้อความแจ้งเตือนที่ส่งผ่าน APNs จะมีรูปแบบเพย์โหลด APNs ดังนี้

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

จัดการข้อความเมื่อปิดใช้การแลกเปลี่ยนเมธอด

โดยค่าเริ่มต้น หากคุณกำหนดคลาสผู้รับมอบสิทธิ์ของแอปให้กับพร็อพเพอร์ตี้ผู้รับมอบสิทธิ์ UNUserNotificationCenter และ Messaging แล้ว FCM จะสลับคลาสผู้รับมอบสิทธิ์ของแอปเพื่อเชื่อมโยงโทเค็น FCM กับโทเค็น APNs ของอุปกรณ์โดยอัตโนมัติ และส่งเหตุการณ์การรับการแจ้งเตือน ไปยัง Analytics หากคุณปิดใช้การแลกเปลี่ยนเมธอดอย่างชัดเจน หากคุณกำลังสร้างแอป SwiftUI หรือหากคุณใช้คลาสแยกต่างหากสำหรับผู้รับมอบสิทธิ์ คุณจะต้องดำเนินการทั้ง 2 งานนี้ด้วยตนเอง

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