Firebase Cloud Messaging มีสองวิธีในการกำหนดเป้าหมายข้อความไปยังอุปกรณ์หลายเครื่อง:
- การส่งข้อความตามหัวข้อ ซึ่งช่วยให้คุณสามารถส่งข้อความไปยังอุปกรณ์หลายเครื่องที่เลือกรับหัวข้อใดหัวข้อหนึ่งได้
- การส่งข้อความกลุ่มอุปกรณ์ ซึ่งช่วยให้คุณสามารถส่งข้อความเดียวไปยังหลาย ๆ อินสแตนซ์ของแอพที่ทำงานบนอุปกรณ์ที่อยู่ในกลุ่ม
บทช่วยสอนนี้มุ่งเน้นไปที่การส่งข้อความหัวข้อจากเซิร์ฟเวอร์แอปของคุณโดยใช้ Admin SDK หรือ REST API สำหรับ FCM และรับและจัดการในแอป iOS หน้านี้แสดงขั้นตอนทั้งหมดในการดำเนินการนี้ตั้งแต่การตั้งค่าไปจนถึงการตรวจสอบ - ดังนั้นจึงอาจครอบคลุมขั้นตอนที่คุณได้ดำเนินการไปแล้วหากคุณ ตั้งค่าแอปไคลเอ็นต์ iOS สำหรับ FCM หรือทำตามขั้นตอนเพื่อ ส่งข้อความแรกของคุณ
เพิ่ม Firebase ในโปรเจ็กต์ iOS ของคุณ
ส่วนนี้ครอบคลุมงานที่คุณอาจทำเสร็จแล้วหากคุณเปิดใช้งานฟีเจอร์ Firebase อื่น ๆ สำหรับแอปของคุณแล้ว สำหรับ FCM โดยเฉพาะคุณจะต้อง อัปโหลดคีย์การตรวจสอบสิทธิ์ APN ของคุณ และ ลงทะเบียนสำหรับการแจ้งเตือนระยะไกล
ข้อกำหนดเบื้องต้น
ติดตั้งสิ่งต่อไปนี้:
- Xcode 12.2 หรือใหม่กว่า
- CocoaPods 1.10.0 หรือใหม่กว่า
ตรวจสอบให้แน่ใจว่าโครงการของคุณตรงตามข้อกำหนดเหล่านี้:
- โปรเจ็กต์ของคุณต้องกำหนดเป้าหมายเป็น iOS 10 ขึ้นไป
ตั้งค่า อุปกรณ์ iOS ที่จับ ต้องได้เพื่อเรียกใช้แอปของคุณและทำงานเหล่านี้ให้เสร็จสิ้น:
- รับคีย์การตรวจสอบสิทธิ์การแจ้งเตือนแบบพุชของ Apple สำหรับบัญชีนักพัฒนา Apple ของ คุณ
- เปิดใช้งานการแจ้งเตือนแบบพุชใน XCode ภายใต้ แอพ> ความสามารถ
- ลงชื่อเข้า ใช้ Firebase โดยใช้บัญชี Google ของคุณ
หากคุณยังไม่มีโปรเจ็กต์ Xcode และเพียงแค่ต้องการทดลองใช้ผลิตภัณฑ์ Firebase คุณสามารถดาวน์โหลดหนึ่งใน ตัวอย่างการเริ่มต้นอย่างรวดเร็ว ของเรา
สร้างโครงการ Firebase
ก่อนจะเพิ่ม Firebase ลงในแอป iOS ได้คุณต้องสร้างโปรเจ็กต์ Firebase เพื่อเชื่อมต่อกับแอป iOS ไปที่ ทำความเข้าใจกับโครงการ Firebase เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโครงการ Firebase
ลงทะเบียนแอปของคุณกับ Firebase
หลังจากที่คุณมีโปรเจ็กต์ Firebase คุณสามารถเพิ่มแอป iOS ของคุณลงไปได้
ไปที่ ทำความเข้าใจกับโครงการ Firebase เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับแนวทางปฏิบัติที่ดีที่สุดและข้อควรพิจารณาในการเพิ่มแอปในโครงการ Firebase รวมถึงวิธีจัดการรูปแบบบิวด์หลายรายการ
ไปที่ คอนโซล Firebase
ตรงกลางหน้าภาพรวมโครงการคลิกไอคอน iOS (
) เพื่อเปิดเวิร์กโฟลว์การตั้งค่าหากคุณได้เพิ่มแอปลงในโครงการ Firebase แล้วให้คลิก เพิ่มแอป เพื่อแสดงตัวเลือกแพลตฟอร์ม
ป้อนรหัสบันเดิลของแอปของคุณในฟิลด์ รหัสบันเดิลของ iOS
รหัสบันเดิลจะ ระบุแอปพลิเคชันในระบบนิเวศของ Apple โดยไม่ซ้ำกัน
ค้นหารหัสบันเดิลของคุณ: เปิดโปรเจ็กต์ iOS ของคุณใน Xcode เลือกแอพระดับบนสุดในตัวนำทางโปรเจ็กต์จากนั้นเลือกแท็บ ทั่วไป
ค่าของช่อง Bundle Identifier คือรหัสบันเดิลของ iOS (ตัวอย่างเช่น
com.yourcompany.yourproject
)โปรดทราบว่าค่ารหัสชุดเป็นแบบตรงตามตัวพิมพ์เล็กและใหญ่และไม่สามารถเปลี่ยนแปลงได้สำหรับแอป Firebase iOS นี้หลังจากที่ลงทะเบียนกับโครงการ Firebase ของคุณแล้ว
(ไม่บังคับ) ป้อนข้อมูลแอปอื่น ๆ : ชื่อเล่น แอป และ รหัส App Store
ชื่อเล่นแอป : ตัวระบุความสะดวกภายในที่มองเห็นได้เฉพาะคุณในคอนโซล Firebase
App Store ID : ใช้โดย Firebase Dynamic Links เพื่อ เปลี่ยนเส้นทางผู้ใช้ไปยังหน้า App Store ของคุณ และโดย Google Analytics เพื่อ นำเข้าเหตุการณ์ Conversion ไปยัง Google Ads หากแอปของคุณยังไม่มีรหัส App Store คุณสามารถเพิ่มรหัสได้ในภายหลังใน การตั้งค่าโครงการ ของคุณ
คลิก ลงทะเบียนแอป
เพิ่มไฟล์การกำหนดค่า Firebase
คลิก ดาวน์โหลด GoogleService-Info.plist เพื่อรับไฟล์กำหนดค่า Firebase iOS ของคุณ (
GoogleService-Info.plist
)ไฟล์กำหนดค่า Firebase มีตัวระบุที่ไม่ซ้ำกัน แต่ไม่เป็นความลับสำหรับโครงการของคุณ หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับไฟล์การกำหนดค่านี้โปรดไปที่ ทำความเข้าใจกับโครงการ Firebase
คุณสามารถดาวน์โหลด ไฟล์กำหนดค่า Firebase อีกครั้งได้ทุกเมื่อ
ตรวจสอบว่าชื่อไฟล์กำหนดค่าไม่ได้ต่อท้ายด้วยอักขระเพิ่มเติมเช่น
(2)
ย้ายไฟล์กำหนดค่าของคุณไปยังรูทของโปรเจ็กต์ Xcode ของคุณ หากได้รับแจ้งให้เลือกเพิ่มไฟล์กำหนดค่าให้กับเป้าหมายทั้งหมด
หากคุณมีรหัสบันเดิลหลายรายการในโปรเจ็กต์คุณต้องเชื่อมโยงรหัสบันเดิลแต่ละรายการกับแอปที่ลงทะเบียนในคอนโซล Firebase เพื่อให้แต่ละแอปมีไฟล์ GoogleService-Info.plist
เป็นของตัวเอง
เพิ่ม Firebase SDK ลงในแอปของคุณ
ขอแนะนำให้ใช้ CocoaPods เพื่อติดตั้งไลบรารี Firebase อย่างไรก็ตามหากคุณไม่ต้องการใช้ CocoaPods คุณสามารถ รวมเฟรมเวิร์ก SDK ได้โดยตรง หรือใช้ Swift Package Manager เบต้า
คุณกำลังใช้ ตัวอย่างการเริ่มต้นอย่างรวดเร็ว หรือไม่? มีโครงการ Xcode และ Podfile (พร้อมพ็อด) อยู่แล้ว แต่คุณยังต้อง เพิ่มไฟล์การกำหนดค่า Firebase และ ติดตั้ง พ็อด
สร้าง Podfile หากคุณยังไม่มี:
cd your-project-directory
pod init
ใน Podfile ของคุณให้เพิ่ม Firebase pods ที่คุณต้องการใช้ในแอปของคุณ
คุณสามารถเพิ่ม ผลิตภัณฑ์ Firebase ที่รองรับ ลงในแอป iOS ของคุณได้
เพื่อประสบการณ์ที่ดีที่สุดกับ Firebase Cloud Messaging เราขอแนะนำให้ เปิดใช้ Google Analytics ในโครงการของคุณ นอกจากนี้ในการตั้งค่า Analytics คุณต้องเพิ่ม Firebase SDK สำหรับ Analytics ลงในแอปของคุณ
เปิดใช้งานการวิเคราะห์
# Add the Firebase pod for Google Analytics pod 'Firebase/Analytics'
# Add the pod for Firebase Cloud Messaging pod 'Firebase/Messaging'ไม่ได้เปิดใช้งานการวิเคราะห์
# Add the pod for Firebase Cloud Messaging pod 'Firebase/Messaging'
ติดตั้ง
.xcworkspace
จากนั้นเปิดไฟล์..xcworkspace
เพื่อดูโปรเจ็กต์ใน Xcode:pod install
open your-project.xcworkspace
อัปโหลดคีย์การตรวจสอบสิทธิ์ APN ของคุณ
อัปโหลดคีย์การตรวจสอบสิทธิ์ APN ของคุณไปยัง Firebase หากคุณยังไม่มีคีย์การตรวจสอบสิทธิ์ APN โปรดดูการ กำหนดค่า APN ด้วย FCM
ภายในโปรเจ็กต์ของคุณในคอนโซล Firebase ให้เลือกไอคอนรูปเฟืองเลือก การตั้งค่าโปรเจ็ก ต์จากนั้นเลือกแท็บ Cloud Messaging
ใน คีย์การตรวจสอบสิทธิ์ APN ภายใต้ การกำหนดค่าแอป iOS ให้คลิกปุ่ม อัปโหลด
เรียกดูตำแหน่งที่คุณบันทึกคีย์ของคุณเลือกแล้วคลิก เปิด เพิ่มรหัสคีย์สำหรับคีย์ (มีอยู่ใน ใบรับรองตัวระบุและโปรไฟล์ ใน Apple Developer Member Center ) แล้วคลิก อัปโหลด
เริ่มต้น Firebase ในแอปของคุณ
คุณจะต้องเพิ่มรหัสเริ่มต้น Firebase ในแอปพลิเคชันของคุณ นำเข้าโมดูล Firebase และกำหนดค่าอินสแตนซ์ที่แชร์ดังที่แสดง:
- นำเข้าโมดูล Firebase ใน
UIApplicationDelegate
ของคุณ:รวดเร็ว
import Firebase
วัตถุประสงค์ -C
@import Firebase;
- กำหนดค่าอินสแตนซ์ที่แชร์ของ
FirebaseApp
โดยทั่วไปจะอยู่ในแอปพลิเคชันของแอปของคุณapplication:didFinishLaunchingWithOptions:
method:รวดเร็ว
// Use Firebase library to configure APIs FirebaseApp.configure()
วัตถุประสงค์ -C
// Use Firebase library to configure APIs [FIRApp configure];
ลงทะเบียนสำหรับการแจ้งเตือนระยะไกล
ไม่ว่าจะเมื่อเริ่มต้นหรือเมื่อถึงจุดที่ต้องการในขั้นตอนแอปพลิเคชันของคุณให้ลงทะเบียนแอปของคุณสำหรับการแจ้งเตือนระยะไกล โทรregisterForRemoteNotifications
ตามที่แสดง: รวดเร็ว
if #available(iOS 10.0, *) { // For iOS 10 display notification (sent via APNS) UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) } else { let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } application.registerForRemoteNotifications()
วัตถุประสงค์ -C
if ([UNUserNotificationCenter class] != nil) { // iOS 10 or later // For iOS 10 display notification (sent via APNS) [UNUserNotificationCenter currentNotificationCenter].delegate = self; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { // ... }]; } else { // iOS 10 notifications aren't available; fall back to iOS 8-9 notifications. UIUserNotificationType allNotificationTypes = (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; } [application registerForRemoteNotifications];
สมัครสมาชิกแอปไคลเอนต์สำหรับหัวข้อ
แอปไคลเอ็นต์สามารถสมัครรับหัวข้อที่มีอยู่หรือจะสร้างหัวข้อใหม่ก็ได้ เมื่อแอปไคลเอ็นต์สมัครรับชื่อหัวข้อใหม่ (รายการที่ไม่มีอยู่สำหรับโปรเจ็กต์ Firebase ของคุณ) หัวข้อใหม่ของชื่อนั้นจะถูกสร้างขึ้นใน FCM และไคลเอนต์ใด ๆ ก็สามารถสมัครได้
หากต้องการสมัครรับหัวข้อให้เรียกใช้วิธีการสมัครสมาชิกจากเธรดหลักของแอปพลิเคชันของคุณ (FCM ไม่ปลอดภัยต่อเธรด) หากคำขอสมัครสมาชิกล้มเหลวในตอนแรก FCM จะลองใหม่โดยอัตโนมัติ สำหรับกรณีที่ไม่สามารถดำเนินการสมัครสมาชิกได้การสมัครสมาชิกจะแสดงข้อผิดพลาดที่คุณสามารถจับได้ในตัวจัดการการดำเนินการดังที่แสดง:
รวดเร็ว
Messaging.messaging().subscribe(toTopic: "weather") { error in print("Subscribed to weather topic") }
วัตถุประสงค์ -C
[[FIRMessaging messaging] subscribeToTopic:@"weather" completion:^(NSError * _Nullable error) { NSLog(@"Subscribed to weather topic"); }];
การเรียกนี้ทำให้คำขอแบบอะซิงโครนัสไปยังแบ็กเอนด์ FCM และสมัครไคลเอ็นต์ตามหัวข้อที่กำหนด ก่อนที่จะเรียก subscribeToTopic:topic
ตรวจสอบให้แน่ใจว่าอินสแตนซ์แอปไคลเอ็นต์ได้รับโทเค็นการลงทะเบียนแล้วผ่านการเรียกกลับ didReceiveRegistrationToken
ทุกครั้งที่แอปเริ่มต้น FCM จะตรวจสอบให้แน่ใจว่าได้สมัครรับหัวข้อที่ร้องขอทั้งหมดแล้ว หากต้องการยกเลิกการสมัครให้เรียก unsubscribeFromTopic:topic
และ FCM ยกเลิกการสมัครจากหัวข้อในเบื้องหลัง
รับและจัดการข้อความหัวข้อ
FCM ส่งข้อความหัวข้อในลักษณะเดียวกับข้อความปลายทางอื่น ๆ
ใช้ AppDelegate application:didReceiveRemoteNotification:
ดังแสดง:
รวดเร็ว
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { // 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) completionHandler(UIBackgroundFetchResult.newData) }
วัตถุประสงค์ -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 message ID. if (userInfo[kGCMMessageIDKey]) { NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); } // Print full message. NSLog(@"%@", userInfo); completionHandler(UIBackgroundFetchResultNewData); }
สร้างคำขอส่ง
หลังจากที่คุณสร้างหัวข้อแล้วไม่ว่าจะสมัครใช้งานอินสแตนซ์แอปไคลเอ็นต์กับหัวข้อทางฝั่งไคลเอ็นต์หรือผ่าน เซิร์ฟเวอร์ API คุณสามารถส่งข้อความไปยังหัวข้อได้ หากนี่เป็นครั้งแรกที่คุณสร้างคำขอ FCM โปรดดูคำแนะนำเกี่ยวกับ สภาพแวดล้อมเซิร์ฟเวอร์ของคุณและ FCM สำหรับข้อมูลพื้นฐานและข้อมูลการตั้งค่าที่สำคัญ
ในตรรกะการส่งของคุณบนแบ็กเอนด์ระบุชื่อหัวข้อที่ต้องการดังที่แสดง:
โหนด js
// The topic name can be optionally prefixed with "/topics/".
var topic = 'highScores';
var message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Java
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'
# See documentation on defining a message payload.
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
topic=topic,
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
ไป
// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Topic: topic,
}
// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
ค#
// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";
// See documentation on defining a message payload.
var message = new Message()
{
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
Topic = topic,
};
// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
}
คำสั่ง cURL:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"message": {
"topic" : "foo-bar",
"notification": {
"body": "This is a Firebase Cloud Messaging Topic Message!",
"title": "FCM Message"
}
}
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
เพื่อส่งข้อความไปยังการรวมกันของหัวข้อระบุเป็นเงื่อนไขซึ่งเป็นนิพจน์บูลีนที่ระบุหัวข้อเป้าหมาย ตัวอย่างเช่นเงื่อนไขต่อไปนี้จะส่งข้อความไปยังอุปกรณ์ที่สมัครสมาชิก TopicA
และ TopicB
หรือ TopicC
:
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
ก่อนอื่น FCM จะประเมินเงื่อนไขใด ๆ ในวงเล็บจากนั้นจึงประเมินนิพจน์จากซ้ายไปขวา ในนิพจน์ข้างต้นผู้ใช้ที่สมัครเป็นสมาชิกหัวข้อใดหัวข้อหนึ่งจะไม่ได้รับข้อความ ในทำนองเดียวกันผู้ใช้ที่ไม่ได้สมัครสมาชิก TopicA
จะไม่ได้รับข้อความ ชุดค่าผสมเหล่านี้จะได้รับ:
-
TopicA
และTopicB
-
TopicA
และTopicC
คุณสามารถรวมหัวข้อได้สูงสุดห้าหัวข้อในนิพจน์เงื่อนไขของคุณ
เพื่อส่งไปยังเงื่อนไข:
โหนด js
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
condition: condition
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Java
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle("$GOOG up 1.43% on the day")
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
.build())
.setCondition(condition)
.build();
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"
# See documentation on defining a message payload.
message = messaging.Message(
notification=messaging.Notification(
title='$GOOG up 1.43% on the day',
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
),
condition=condition,
)
# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
ไป
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Condition: condition,
}
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
ค#
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = new Message()
{
Notification = new Notification()
{
Title = "$GOOG up 1.43% on the day",
Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
},
Condition = condition,
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
ส่วนที่เหลือ
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
คำสั่ง cURL:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"notification": {
"title": "FCM Message",
"body": "This is a Firebase Cloud Messaging Topic Message!",
},
"condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
ขั้นตอนถัดไป
- คุณสามารถใช้เซิร์ฟเวอร์ของคุณเพื่อสมัครอินสแตนซ์แอปไคลเอ็นต์กับหัวข้อและดำเนินการจัดการอื่น ๆ โปรดดู จัดการการสมัครสมาชิกหัวข้อบนเซิร์ฟเวอร์
- เรียนรู้เพิ่มเติมเกี่ยวกับวิธีอื่นในการส่งไปยังอุปกรณ์หลายเครื่อง - การส่งข้อความกลุ่มอุปกรณ์