[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-09 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nDevice group messaging lets you add multiple devices to a single group. This is\nsimilar to topic messaging, but includes authentication to ensure that group\nmembership is managed only by your servers. For example, if you want to send\ndifferent messages to different phone models, your servers can add or remove\nregistrations to the appropriate groups and send the appropriate message to each\ngroup. Device group messaging differs from topic messaging in that it involves\nmanaging device groups from your servers instead of directly within your\napplication.\n\nThe maximum number of members allowed for a notification key is 20.\n\nManaging device groups\n\nBefore sending messages to a device group, you must:\n\n1. Obtain registration tokens for each device you want to add to the group.\n2. Create the `notification_key`, which identifies the device group by mapping a particular group (typically a user) to all of the group's associated registration tokens. You can create notification keys on the app server.\n\nBasic management of device groups --- creating and removing groups, and adding or\nremoving devices --- is performed using the HTTP v1 API, using short-lived tokens to\n[authorize send requests](/docs/cloud-messaging/auth-server). See [Device group\nmanagement keys](#device-group-management-keys) for a list of supported keys.\n\nManaging device groups on the app server\n\nCreating a device group\n\nTo create a device group, send a POST request that provides a name for the\ngroup, and a list of registration tokens for the devices. FCM returns\na new `notification_key` that represents the device group.\n\nHTTP POST request\n\nSend a request like the following to\n`https://fcm.googleapis.com/fcm/notification`: \n\n https://fcm.googleapis.com/fcm/notification\n Content-Type:application/json\n access_token_auth: true\n Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA\n project_id:SENDER_ID\n\n {\n \"operation\": \"create\",\n \"notification_key_name\": \"appUser-Chris\",\n \"registration_ids\": [\"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...\",\n \"cR1rjyj4_Kc:APA91bGusqbypSuMdsh7jSNrW4nzsM...\",\n ... ]\n }\n\nThe `notification_key_name` is a name or identifier (e.g., it can be a\nusername) that is unique to a given group. The `notification_key_name` and\n`notification_key` are unique to a group of registration tokens. It is\nimportant that `notification_key_name` is unique per client app if you have\nmultiple client apps for the same [sender ID](/docs/cloud-messaging/concept-options#senderid).\nThis ensures that messages only go to the intended target app.\n\nResponse format\n\nA successful request returns a `notification_key` like the following: \n\n {\n \"notification_key\": \"APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ\"\n }\n\nSave the `notification_key` and the corresponding `notification_key_name` to\nuse in subsequent operations.\n| **Caution:** Invalid registration tokens are dropped when a device group is successfully created.\n\nRetrieving a notification key\n\nIf you need to retrieve an existing notification key, use the\n`notification_key_name` in a GET request as shown: \n\n https://fcm.googleapis.com/fcm/notification?notification_key_name=appUser-Chris\n Content-Type:application/json\n access_token_auth: true\n Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA\n project_id:SENDER_ID\n {}\n\nFor each GET request for a given notification key name, the server returns a\nunique encoded string. Though each string may appear to be a different key,\nit is actually a valid `notification_key` value.\n\nAdding and removing devices from a device group\n\nTo add or remove devices from an existing group, send a POST request with the\n`operation` parameter set to `add` or `remove`, and provide the registration\ntokens for addition or removal.\n| **Caution:** If you remove all existing registration tokens from a device group, FCM deletes the device group.\n\nHTTP POST request\n\nFor example, to add a device with the registration token\n`bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...` to `appUser-Chris`, you would\nsend this request: \n\n https://fcm.googleapis.com/fcm/notification\n Content-Type:application/json\n access_token_auth: true\n Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA\n project_id:SENDER_ID\n\n {\n \"operation\": \"add\",\n \"notification_key_name\": \"appUser-Chris\",\n \"notification_key\": \"APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ\",\n \"registration_ids\": [\"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...\"]\n }\n\nResponse format\n\nA successful request to either add or remove a device returns a\n`notification_key` like the following: \n\n { \"notification_key\": \"APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ\" }\n\n| **Note:** `notification_key_name` is not required for adding or removing registration tokens, but including it protects you against accidentally using the incorrect `notification_key`.\n\nDevice group management keys\n\nThe following table lists the keys for creating device groups and adding and\nremoving members.\n\n| Parameter | Usage | Description |\n|-------------------------|--------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `operation` | Required, string | The operation to run.Valid values are `create`, `add`, and `remove`. |\n| `notification_key_name` | Required, string | The user-defined name of the device group to create or modify. |\n| `notification_key` | Required (except for `create` operation, string) | Unique identifier of the device group. This value is returned in the response for a successful `create` operation, and is required for all subsequent operations on the device group. |\n| `registration_ids` | Required, array of strings | The device tokens to add or remove. If you remove all existing registration tokens from a device group, FCM deletes the device group. |\n\nSending messages to device groups\n\nSending messages to a device group is very similar to sending\nmessages to an individual device, using the same method to\n[authorize send requests](/docs/cloud-messaging/auth-server). Set the `token`\nfield to the group notification key: \n\nREST \n\n POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1\n\n Content-Type: application/json\n Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA\n\n {\n \"message\":{\n \"token\":\"APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ\",\n \"data\":{\n \"hello\": \"This is a Firebase Cloud Messaging device group message!\"\n }\n }\n }\n\ncURL command \n\n curl -X POST -H \"Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA\" -H \"Content-Type: application/json\" -d '{\n \"message\":{\n \"data\":{\n \"hello\": \"This is a Firebase Cloud Messaging device group message!\"\n },\n \"token\":\"APA91bGHXQBB...9QgnYOEURwm0I3lmyqzk2TXQ\"\n }}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send"]]