通知(本地和 Firebase)

类型
修订版 版本 2024.3703
关键词 通知
平台 Android,iOS
另请参阅 本地/推送通知 (指南)

概述

此插件提供对本地通知和 Firebase 推送通知的访问。

重要

对于 Android,所有新的应用或首次使用推送通知更新的现有应用必须使用Firebase 云消息传递 (FCM),此插件支持该功能。如果您有一个已为Google 云消息传递 (GCM)配置的旧版应用,它将继续无限期地工作,但您应该继续使用旧版插件而不是此插件。

重要

要在 iOS 上使用 Firebase 通知,请确保在您的 `build.settings` 中包含 `plugin.notifications.v2.firebase` 插件。此插件与 `plugin.notifications.v2` 相同,并且还包含在 iOS 上通过 Firebase 操作通知所需的库。请注意,要在 iOS 上使用 Firebase,您需要使用必要的 Apple 推送通知身份验证或密钥配置您的 Firebase 项目,了解更多

语法

local notifications = require( "plugin.notifications.v2" )

函数

notifications.subscribe()

notifications.unsubscribe()

notifications.scheduleNotification()

notifications.cancelNotification()

notifications.getDeviceToken()

notifications.registerForPushNotifications()

项目设置

要使用此插件,请在 `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

如果您的应用适用于 iOS,则必须在`notification` → `iphone` → `types``config.lua` 表中指定您的应用将使用的通知类型

application =
{
    notification =
    {
        iphone =
        {
            types =
            {
                "badge",
                "sound",
                "alert"
            },
        },
    },
}

此外,如果您想使用 GoogleFirebase 云消息传递 (FCM)进行推送通知,请按照以下步骤操作

  1. 将 Firebase 控制台中提供的 `GoogleService-Info.plist` 复制到您的 Corona 项目的根目录,与 `main.lua` 放在一起。

  2. 将以下条目添加到`iphone` → `plist``build.settings` 表中

settings =
{
    iphone =
    {
        plist =
        {
            UIBackgroundModes = { "remote-notification" },
            FirebaseAppDelegateProxyEnabled = false,
        },
    },
}

Android

如果您的应用适用于 Android,则必须按照以下附加步骤操作,以确保 Firebase 通知正常运行

  1. 复制`google-services.json`(在 Firebase 控制台中提供)到您的 Corona 项目的根目录,与 `main.lua` 放在一起。

  2. 在 `build.settings` 的 `android` 表中添加一个额外的 `useGoogleServicesJson` 条目。添加后,构建服务器将从 JSON 文件中读取设置,并在构建阶段将它们集成到您的应用中。

settings =
{
    android =
    {
        useGoogleServicesJson = true,
    },
}

Solar2D Native

iOS

如果您在 iOS 上使用 Solar2D Native,则需要在您的 `Info.plist` 中添加对 `CoronaNotificationsDelegate` 的引用,如下所示

  1. 在您的 `Info.plist` 中添加一个名为 `"CoronaDelegates"` 的条目,并将其类型设为 `Array`。

  2. 向 `CoronaDelegates` 数组添加一个条目,并将其命名为 `"CoronaNotificationsDelegate"`。确保其类型为 `String`。

Android

在 Android 上进行原生开发时,Google 要求项目 `res` 目录中的所有图标。这很重要,因为 Google 的 Android 构建过程会生成代码,创建一个 `R.java` 文件,其中包含项目及其库中每个资源的唯一整数 ID。需要使用这些 ID 才能访问这些资源,包括通知图标。

使用 Solar2D Native 进行开发时,您必须按如下方式覆盖 Corona 的通知图标资源

  1. 首先,您应该通过阅读 Google 的官方文档来熟悉 Android 如何处理资源。

  2. 检查 Corona 库的资源目录 (`./Native/Corona/android/lib/Corona/res/`)。在此目录中,每个 `drawable` 目录都包含一个名为 `corona_statusbar_icon_default.png` 的文件。这是要覆盖的图像文件。请注意,此操作应该是“覆盖”而不是替换 — 您不应修改 Corona 库目录的内容!

  3. 要用您自己的通知图像文件覆盖这些文件,您需要在 `res` 目录下设置具有相同 `drawable` 目录的 Android 项目。通过分辨率和版本将每个 `IconNotification*.png` 图标添加到其各自的 `drawable` 目录中(请参阅此处。您还必须将这些文件重命名为 `corona_statusbar_icon_default.png`。

  4. `build.gradle` (`Module: app`) 的 `copyCoronaResources` 任务进行少量更改以忽略 Corona 库项目中提供的通知图标。通过将`from fileTree(...)`行替换为以下内容来执行此操作

from fileTree(dir: "$coronaEnterpriseAndroidLibDir/res", exclude: '**/corona_statusbar_icon_default.png')