notifications.*

类型
版本 版本 2024.3703
关键词 通知
平台 Android、iOS

概述

提供对本地和推送通知的访问。

本文档概述了旧版通知插件,主要用于已配置Google 云消息传递 (GCM)的 Android 应用。如果您正在创建新的 Android 应用或首次为现有 Android 应用更新推送通知,则必须使用Firebase 云消息传递 (FCM),它受当前 通知 插件支持。

对于新的 iOS 应用或首次使用通知更新的现有 iOS 应用,您还应该实施当前的 通知 插件。

语法

local notifications = require( "plugin.notifications" )

函数

notifications.registerForPushNotifications()

notifications.cancelNotification()

notifications.scheduleNotification()

项目设置

要使用此插件,请在 `build.settings` 的 `plugins` 表中添加一个条目。添加后,构建服务器将在构建阶段集成该插件。

settings =
{
    plugins =
    {
        ["plugin.notifications"] =
        {
            publisherId = "com.coronalabs"
        },
    },
}

Android

如果您的应用适用于 Android,并且您打算使用 GCM 推送通知,则还需要包含以下插件。但是,如果您只打算使用本地通知,则应省略此插件。

settings =
{
    plugins =
    {
        ["shared.google.play.services.gcm"] =
        {
            publisherId = "com.coronalabs",
            supportedPlatforms = { android=true }
        },
    },
}

此外,请在您的 `config.lua` 文件中添加一个 `notification` 表,如下所示。当您向 GCM 服务注册时,您应该收到 `google` 表所需的 `projectNumber` 值。

application =
{
    notification =
    {
        google =
        {
            projectNumber = "YOUR_PROJECT_NUMBER"
        },
    }
}

Solar2D Native

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

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

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