Google 课堂插件现已面向开发者正式推出!如需了解详情,请参阅
插件文档。
通过 Classroom API 创建推送通知
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以使用 Registrations
集合的方法,在 Google 课堂中的数据发生变化时接收通知。
本文简要介绍了相关概念,并提供了有关如何开始接收推送通知的简单说明。
Google 课堂推送通知概览
借助 Classroom API 推送通知功能,使用 Classroom API 的应用可以在 Google 课堂中数据发生变化时订阅通知。通知会传送到 Cloud Pub/Sub 主题,通常会在更改发生后的几分钟内传送。
如需接收推送通知,您需要设置 Cloud Pub/Sub 主题,并在为相应的通知 Feed 创建注册时提供该主题的名称。
下面列出了本文档中使用的关键概念的定义:
- 目标位置是指通知的发送位置。
- Feed 是一种第三方应用可以订阅的通知。例如,“课程 1234 的学生名单更改”。
- 注册是指向 Google 课堂 API 的指令,用于将特定 Feed 中的通知传送到目标。
为 Feed 创建注册后,该注册的 Cloud Pub/Sub 主题会收到来自该 Feed 的通知,直到该注册过期为止。注册有效期为 1 周,但您可以在到期前随时向 registrations.create()
发出相同的请求来延长注册有效期。
您的 Cloud Pub/Sub 主题仅会收到与您可以使用创建注册时提供的凭据查看的资源相关的通知。例如,如果用户撤消对您应用的权限或被移除教师身份,系统将不再传送通知。
Feed 类型
Classroom API 目前提供三种类型的 Feed:
- 每个网域都有一个网域的学生名单更改 Feed,该 Feed 会在学生和教师加入和退出该网域中的课程时显示通知。
- 每门课程都有一个课程名单更改 Feed,当学生和教师加入和退出课程时,系统会在该 Feed 中显示通知。
- 每个课程都有一个课程作业更改 Feed,当该课程中创建或修改任何课业或学生提交内容对象时,系统会显示通知。
设置 Cloud Pub/Sub 主题
系统会将通知传送到 Cloud Pub/Sub 主题。您可以通过 Cloud Pub/Sub 在 webhook 上接收通知,也可以通过轮询订阅端点接收通知。
如需设置 Cloud Pub/Sub 主题,您需要执行以下操作:
- 确保您满足 Cloud Pub/Sub 前提条件。
- 设置 Cloud Pub/Sub 客户端。
- 查看 Cloud Pub/Sub 价格,并为您的 Play 管理中心项目启用结算功能。
您可以在开发者控制台中(最简单)、通过命令行工具(适用于简单的程序化使用)或使用 Cloud Pub/Sub API 创建 Cloud Pub/Sub 主题。请注意,Cloud Pub/Sub 仅允许使用数量有限的主题,因此使用一个主题接收所有通知可确保您的应用在用户众多时不会遇到扩缩问题。
在 Cloud Pub/Sub 中创建订阅,以告知 Cloud Pub/Sub 如何传送通知。
最后,在注册推送通知之前,您需要向推送通知服务账号 (classroom-notifications@system.gserviceaccount.com
) 授予向您的主题发布消息的权限。
注意:如果您向推送通知服务账号授予向 Cloud Pub/Sub 主题发布消息的权限,那么能够从您的 Play 管理中心项目发出请求的用户将能够确定该主题是否存在,并注册接收该主题的通知。许多应用会在客户端存储 OAuth 客户端 ID,因此最终用户或许能够通过您的 Play 管理中心项目发出请求。如果您遇到这种情况,并且担心最终用户向您的 Cloud Pub/Sub 主题发送不需要的通知,或者知道您用于推送通知的 Cloud Pub/Sub 主题的名称,则应考虑通过其他 Play 管理中心项目注册接收推送通知。
注册应用以接收通知
创建一个 Classroom API 推送通知服务账号可以发布到其中的主题后,您就可以使用 registrations.create()
方法注册接收通知。registrations.create()
方法用于验证推送通知服务账号是否可以访问所提供的 Cloud Pub/Sub 主题。如果推送通知服务账号无法访问主题(例如,主题不存在或您未向其授予相应主题的发布权限),则该方法会失败。
授权
与对 Classroom API 的所有调用一样,对 registrations.create()
的调用必须使用授权令牌进行授权。此身份验证令牌必须包含“推送通知”范围 (https://www.googleapis.com/auth/classroom.push-notifications
) 以及查看正在发送哪些通知的数据所需的所有范围。
- 对于名单更改 Feed,这意味着“名单”范围或(最好是)其只读变体(
https://www.googleapis.com/auth/classroom.rosters.readonly
或 https://www.googleapis.com/auth/classroom.rosters
)。
- 对于课业更改 Feed,这意味着课业范围的“学生”版本,或者(理想情况下)其只读变体 (
https://www.googleapis.com/auth/classroom.coursework.students.readonly
或 https://www.googleapis.com/auth/classroom.coursework.students
)。
如需传送通知,应用必须保留来自具有所需范围的已获授权用户的 OAuth 授予。如果用户断开与应用的连接,则通知会停止。请注意,目前不支持出于此目的进行全网域授权。如果您尝试仅使用域名级委托授权注册接收通知,则会收到 @MissingGrant
错误。
接收通知
通知采用 JSON 编码,并包含:
以下代码段演示了示例通知:
{
"collection": "courses.students",
"eventType": "CREATED",
"resourceId": {
"courseId": "12345",
"userId": "45678"
}
}
通知还具有 registrationId
消息属性,其中包含导致通知的注册的标识符,该标识符可与 registrations.delete()
搭配使用,以取消注册通知。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-05-26。
[null,null,["最后更新时间 (UTC):2025-05-26。"],[],[],null,["# Push notifications in the Classroom API\n\nYou can use the methods on the\n[`Registrations`](/workspace/classroom/reference/rest/v1/registrations)\ncollection to receive notifications when data changes in Classroom.\n\nThis article provides a conceptual overview along with simple instructions on\nhow to start receiving push notifications.\n\nOverview of Classroom push notifications\n----------------------------------------\n\nThe Classroom API push notifications feature allows applications using the\nClassroom API to subscribe for notifications when data changes in Classroom.\nNotifications are delivered to a [Cloud\nPub/Sub](https://cloud.google.com/pubsub) topic, usually within a few minutes of\nthe change.\n\nTo receive push notifications, you need to [set up a Cloud Pub/Sub\ntopic](#topic-setup) and provide that topic's name when you create a\n**registration** for the appropriate **feed** of notifications.\n\nBelow are definitions of key concepts used in this documentation:\n\n- A **destination** is a place where notifications are sent.\n- A **feed** is a type of notifications that a third party application can subscribe to. For example, \"roster changes for course 1234\".\n- A **registration** is an instruction to the Classroom API to deliver notifications from a particular *feed* to a *destination*.\n\nOnce you create a registration for a feed, that registration's Cloud Pub/Sub\ntopic receives notifications from that feed until it expires. Your registration\nlasts a week, but you can extend it at any point before it expires by making an\nidentical request to `registrations.create()`.\n\nYour Cloud Pub/Sub topic only receives notifications about resources that you\ncan view with the credentials you supply when creating a registration. For\nexample, if the user revokes permission from your application or is removed as a\nteacher, notifications are no longer delivered.\n\n### Types of feeds\n\nThe Classroom API currently offers three types of feed:\n\n- Each domain has a **roster changes for domain** feed, which exposes notifications when students and teachers join and leave courses in that domain.\n- Each course has a **roster changes for course** feed, which exposes notifications when students and teachers join and leave courses in that course.\n- Each course has a **course work changes for course** feed, which exposes notifications when any course work or student submission objects are created or modified in that course.\n\nSetting up a Cloud Pub/Sub topic\n--------------------------------\n\nNotifications are delivered to Cloud Pub/Sub topics. From Cloud Pub/Sub, you can\nreceive notifications on a webhook, or by polling a subscription endpoint.\n\nTo set up a Cloud Pub/Sub topic, you need to do the following:\n\n1. Make sure you fulfill the [Cloud Pub/Sub\n Prerequisites](https://cloud.google.com/pubsub/prereqs).\n2. [Set up a Cloud Pub/Sub client](https://cloud.google.com/pubsub/configure).\n3. Review the [Cloud Pub/Sub pricing](https://cloud.google.com/pubsub/pricing), and enable billing for your Developer Console project.\n4. Create a Cloud Pub/Sub topic [in the Developer\n Console](https://cloud.google.com/pubsub/docs/quickstart-console#create_a_topic)\n (easiest), [via the command line\n tool](https://cloud.google.com/pubsub/docs/quickstart-cli#use_the_gcloud_command-line_tool)\n (for simple programmatic use) or using the [Cloud Pub/Sub\n API](https://cloud.google.com/pubsub/docs/quickstart-client-libraries#create_a_topic_and_a_subscription).\n Note that Cloud Pub/Sub [only allows a limited number of\n topics](https://cloud.google.com/pubsub/quotas), so using one topic to\n receive all your notifications ensures you do not hit scaling issues if your\n application becomes popular.\n\n5. [Create a Subscription in Cloud\n Pub/Sub](https://cloud.google.com/pubsub/docs/subscriber), to tell Cloud\n Pub/Sub how to deliver your notifications.\n\n6. Finally, before registering for Push Notifications, you need to [grant the\n Push Notifications service account\n (`classroom-notifications@system.gserviceaccount.com`)\n permission](https://cloud.google.com/pubsub/docs/access_control#console) to\n publish to your topic.\n\nNOTE: If you grant the Push Notifications service account permission to publish\nto your Cloud Pub/Sub topic, users who can make requests from your Developer\nConsole project will be able to determine that it exists, and register for\nnotifications to it. Many applications store OAuth Client IDs on the client\nside, so end-users might be able to make requests from your Developer Console\nproject. If this applies to you, and you are concerned about end-users sending\nunwanted notifications to your Cloud Pub/Sub topic or knowing the names of Cloud\nPub/Sub topics that you use for push notifications, you should consider\nregistering for push notifications from a different Developer Console project.\n\nRegister your application for notifications\n-------------------------------------------\n\nOnce you have a topic that the Classroom API push notifications service account\ncan publish to, you can register for notifications, using the\n[`registrations.create()`](/workspace/classroom/reference/rest/v1/registrations/create)\nmethod. The `registrations.create()` method validates that the provided Cloud\nPub/Sub topic can be reached by the push notifications service account. The\nmethod fails if the push notifications service account cannot reach the topic;\nfor example, if the topic does not exist or you have not granted it publish\npermission on that topic.\n\n### Authorization\n\nLike all calls to the Classroom API, calls to `registrations.create()` [must be\nauthorized](/workspace/classroom/guides/auth) with an authorization token. This\nauthentication token must include the Push Notifications scope\n(`https://www.googleapis.com/auth/classroom.push-notifications`) and whatever\nscopes are required to view the data about which notifications are being sent.\n\n- For roster change feeds, this means the Rosters scope or (ideally) its read-only variant (`https://www.googleapis.com/auth/classroom.rosters.readonly` or `https://www.googleapis.com/auth/classroom.rosters`).\n- For course work change feeds, this means the \"students\" versions of the course work scope or (ideally) its read-only variant (`https://www.googleapis.com/auth/classroom.coursework.students.readonly` or `https://www.googleapis.com/auth/classroom.coursework.students`).\n\nFor notifications to be delivered, the application must retain an OAuth grant\nfrom the authorized user with the required scopes. If the user disconnects the\napplication, notifications cease. Note that currently, domain-wide delegation of\nauthority is not supported for this purpose. If you attempt to register for\nnotifications using only domain-wide delegated authority, you will receive a\n`@MissingGrant` error.\n\nReceiving notifications\n-----------------------\n\nNotifications are encoded with JSON, and contain:\n\n- The name of the collection containing the resource that changed. For notifications about roster changes, this is either `courses.students` or `courses.teachers`. For course work changes, this is either `courses.courseWork` or `courses.courseWork.studentSubmissions`.\n- Identifiers for the resource that changed, in a map. This map is designed to match the arguments to the `get` method of the appropriate resource. For notifications about roster changes, the `courseId` and `userId` fields will be populated, and can be sent unmodified to [courses.students.get()](/workspace/classroom/reference/rest/v1/courses.students/get) or [courses.teachers.get()](/workspace/classroom/reference/rest/v1/courses.teachers/get). Similarly, changes to the courses.courseWork collection will have `courseId` and `id` fields that can be sent unmodified to [courses.courseWork.get()](/workspace/classroom/reference/rest/v1/courses.courseWork/get) and changes to the courses.courseWork.studentSubmissions collection will have `courseId`, `courseWorkId`, and `id` fields that can be sent unmodified to [courses.courseWork.studentSubmissions.get()](/workspace/classroom/reference/rest/v1/courses.courseWork.studentSubmissions/get).\n\nThe following code snippet demonstrates a sample notification: \n\n {\n \"collection\": \"courses.students\",\n \"eventType\": \"CREATED\",\n \"resourceId\": {\n \"courseId\": \"12345\",\n \"userId\": \"45678\"\n }\n }\n\nNotifications also have a `registrationId` message attribute, containing the\nidentifier for the registration that caused the notification, which can be used\nwith\n[`registrations.delete()`](/workspace/classroom/reference/rest/v1/registrations/delete)\nto unregister from notifications."]]