类型 库 修订版 版本 2024.3703 关键词 通知 平台 Android,iOS 另请参阅 本地/推送通知 (指南)
此插件提供对本地通知和 Firebase 推送通知的访问。
对于 Android,所有新的应用或首次使用推送通知更新的现有应用必须使用
要在 iOS 上使用 Firebase 通知,请确保在您的 `build.settings` 中包含 `plugin.notifications.v2.firebase` 插件。此插件与 `plugin.notifications.v2` 相同,并且还包含在 iOS 上通过 Firebase 操作通知所需的库。请注意,要在 iOS 上使用 Firebase,您需要使用必要的 Apple 推送通知身份验证或密钥配置您的 Firebase 项目,了解更多。
local notifications = require( "plugin.notifications.v2" )
要使用此插件,请在 `build.settings` 的 `plugins` 表中添加一个条目。添加后,构建服务器将在构建阶段集成该插件。
settings = { plugins = { ["plugin.notifications.v2"] = { publisherId = "com.coronalabs" }, }, }
如果您计划在 iOS 上使用 Firebase,请使用 `plugin.notifications.v2.firebase`。
settings = { plugins = { ["plugin.notifications.v2.firebase"] = { publisherId = "com.coronalabs" }, }, }
如果您的应用适用于 iOS,则必须在
application = { notification = { iphone = { types = { "badge", "sound", "alert" }, }, }, }
此外,如果您想使用 Google
将 Firebase 控制台中提供的 `GoogleService-Info.plist` 复制到您的 Corona 项目的根目录,与 `main.lua` 放在一起。
将以下条目添加到
settings = { iphone = { plist = { UIBackgroundModes = { "remote-notification" }, FirebaseAppDelegateProxyEnabled = false, }, }, }
如果您的应用适用于 Android,则必须按照以下附加步骤操作,以确保 Firebase 通知正常运行
复制`google-services.json`
在 `build.settings` 的 `android` 表中添加一个额外的 `useGoogleServicesJson` 条目。添加后,构建服务器将从 JSON 文件中读取设置,并在构建阶段将它们集成到您的应用中。
settings = { android = { useGoogleServicesJson = true, }, }
如果您在 iOS 上使用 Solar2D Native,则需要在您的 `Info.plist` 中添加对 `CoronaNotificationsDelegate` 的引用,如下所示
在您的 `Info.plist` 中添加一个名为 `"CoronaDelegates"` 的条目,并将其类型设为 `Array`。
向 `CoronaDelegates` 数组添加一个条目,并将其命名为 `"CoronaNotificationsDelegate"`。确保其类型为 `String`。
在 Android 上进行原生开发时,Google 要求项目 `res` 目录中的所有图标。这很重要,因为 Google 的 Android 构建过程会生成代码,创建一个 `R.java` 文件,其中包含项目及其库中每个资源的唯一整数 ID。需要使用这些 ID 才能访问这些资源,包括通知图标。
使用 Solar2D Native 进行开发时,您必须按如下方式覆盖 Corona 的通知图标资源
首先,您应该通过阅读 Google 的官方文档来熟悉 Android 如何处理资源。
检查 Corona 库的资源目录 (`./Native/Corona/android/lib/Corona/res/`)。在此目录中,每个 `drawable` 目录都包含一个名为 `corona_statusbar_icon_default.png` 的文件。这是要覆盖的图像文件。请注意,此操作应该是“覆盖”而不是替换 — 您不应修改 Corona 库目录的内容!
要用您自己的通知图像文件覆盖这些文件,您需要在 `res` 目录下设置具有相同 `drawable` 目录的 Android 项目。通过分辨率和版本将每个 `IconNotification*.png` 图标添加到其各自的 `drawable` 目录中
对`from fileTree(...)`
from fileTree(dir: "$coronaEnterpriseAndroidLibDir/res", exclude: '**/corona_statusbar_icon_default.png')