jmmorillon/expo-notifications-bundle

用于处理 expo react-native 框架推送通知服务后端任务的组件包。

安装: 2

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 17

类型:symfony-bundle

1.2.2 2021-05-14 12:56 UTC

This package is auto-updated.

Last update: 2024-09-14 21:33:53 UTC


README

该组件包的目的是处理 expo react-native 框架推送通知服务的后端任务。

安装

使用 composer 安装组件包

composer require solvecrew/expo-notifications-bundle

在 app/AppKernel.php 文件中启用组件包

$bundles = [
    ...
    new Solvecrew\ExpoNotificationsBundle\SCExpoNotificationsBundle(),
    ...
];

配置

目前,该组件包只有一个可选的配置参数。

如果您想(可选),将以下内容添加到您的 app/config/config.yml 文件中

sc_expo_notifications:
    expo_api_endpoint: '%expo_api_endpoint%'

然后,在 app/config/parameters.yml 文件中添加 expo_api_endpoint 参数

expo_api_endpoint: https://exp.host/--/api/v2/push/send

如果您不想在 parameters.yml 文件中添加作为参数,您可以直接在 config.yml 文件中添加 URI

sc_expo_notifications:
    expo_api_endpoint: https://exp.host/--/api/v2/push/send

重要:所有这些都是可选的。如果您根本不添加配置,它将使用官方 Expo 文档中的端点 https://exp.host/--/api/v2/push/send 作为回退。

使用方法

此组件包提供了一种简单的方法来使用 Expo React-Native 框架向前端应用发送推送通知。因此,组件包提供了一些有用的功能

  • NotificationContentModel:一个表示单个通知请求数据的模型。如 Expo API 所述。
  • NotificationManager:一个管理器,用于处理通知的准备工作、发送和响应。

NotificationManager 的服务是 sc_expo_notifications.notification_manager

  • 在控制器中使用 $this->container->get('sc_expo_notifications.notification_manager')
  • 将其作为依赖项注入
app.example_manager:
    class: AcmeBundle\Manager\ExampleManager
    arguments: ['@sc_expo_notifications.notification_manager']

注意:这里的重要部分当然是 arguments: ['@sc_expo_notifications.notification_manager'],当然。

在您获得 NotificationManager 后,可以访问其功能。常用的功能有

  1. sendNotifications(...): 在一个 API 请求中发送多个通知。
    /**
     * Handle the overall process of multiple new notifications.
     *
     * @param array $messages
     * @param array $tokens
     * @param array $titles
     * @param array $data
     *
     * @return array
     */
    public function sendNotifications(
        array $messages,
        array $tokens,
        array $titles = [],
        array $data = []
    ): array
    {
		...
    }

因此,您需要提供一个字符串数组 messages 和一个字符串数组 tokens(更具体地说:收件人的 ExponentPushToken,如 sITGtlHf1-mSgUyQIVbVMJ,不带 ExponentPushToken[] 包围)。messages 数组中的第一个消息将发送到 tokens 数组中第一个 token(收件人)。以此类推。可选地,您可以提供一个包含通知标题的 titles 数组。最后,您可以提供一个包含要添加到通知中的 JSON 对象的数据数组的数组,以便在前端进一步处理。重要的是要知道,每个通知都需要一个数组作为数据!请参阅下面的完整示例以获取更多信息。

此函数返回一个包含 NotificationContentModel 的数组。每个被尝试发送的通知都有一个。这些 NotificationContentModel 包含有关通知的所有信息。

例如

  • to:表示收件人的 token。
  • title:如果提供,则表示标题。
  • body:通知的实际消息。
  • wasSuccessful:一个布尔值,表示通知是否已发送(并不意味着它已被接收或查看)。
  • responseMessage:在针对特定通知的请求失败时,由 Expo API 返回的消息。
  • responseDetails:包含错误特定信息的数组。
  1. sendNotification(...): 提供一个消息字符串和一个 token 发送单个通知。可选地提供标题。
    /**
     * Handle the overall process of a new notification.
     *
     * @param string $message
     * @param string $token
     * @param string $title
     * @param array $data
     *
     * @return array
     */
    public function sendNotification(
        string $message,
        string $token,
        string $title = '',
        array $data = null
    ): NotificationContentModel
    {
		...
    }

如您所见,这个非常简单直接。它返回一个上面描述的单一NotificationContentModel。标题(字符串)和数据(数组)是可选的。如果提供,$data必须是一个数组。

完整示例

为了进一步简化集成过程,请参考以下示例。

// Get an instance of the NotificationManager provided by this bundle.
// Using the service, that is available since the bundle installation.
// Better would be to inject the service as a dependency in your service configuration.
$notificationManager = $this->get('sc_expo_notifications.notification_manager');

// Prepare the titles as you wish. If none would be provided, the app name will be a fallback by Expo.
$titles = [
    'New Notification',
    'Hot news',
];

// Prepare the messages that shall be sent. This will be more sophisticated under realistic circumstances...
$messages = [
    'Hello there!',
    'What's up?!',
];

// Prepare the ExpoPushTokens of the recipients.
$tokens = [
    'H-Dsb2ATt2FHoD_5rVG5rh',
    'S_Fs-1ATt4AHDD_5rXcYr4',
];

// Prepare the data that you want to pass to the front-end to help you handle the notification.
$data = [
	['foo' => 'bar', 'baz' => 'boom'],
	['whatever' => 'you', 'want' => 'here'],
];

// Send the notifications using the messages and the tokens that will receive them.
$notificationContentModels = $notificationManager->sendNotifications(
    $messages,
    $tokens,
    $titles,
    $data
);

// Handle the response here. Each NotificationContentModel in the $notificationContentModels array
// holds the information about its success/error and more detailed information.

如果您的情况更复杂或您想利用更多通知功能,可以使用NotificationManager的sendNotificationHttp函数。为此,您需要自己创建NotificationContentModel。

// Use statement for the NotificationContentModel.
use Solvecrew\ExpoNotificationsBundle\Model\NotificationContentModel;

// Get an instance of the NotificationManager provided by this bundle.
// Using the service, that is available since the bundle installation.
// Better would be to inject the service as a dependency in your service configuration.
$notificationManager = $this->get('sc_expo_notifications.notification_manager');

$token = 'H-Dsb2ATt2FHoD_5rVG5rh';
$message = 'The message of the notification.';
$data = ['foo' => 'bar'];

$notificationContentModel = new NotificationContentModel();
$notificationContentModel
    ->setTo($token)
    ->setBody($message)
    ->setData($data)
    ->setPriority('medium');

// Send the notification.
$httpResponse = $notificationManager->sendNotificationHttp($notificationContentModel);

// Handle the response using the notificationManager. Enriches the NotifcationContentModel with the http response data.
$notificationContentModel = $notificationManager->handleHttpResponse($httpResponse, [$notificationContentModel]);

如果您想通过这种方式发送多个通知,请使用sendNotificationsHttp(复数)。

// Use statement for the NotificationContentModel.
use Solvecrew\ExpoNotificationsBundle\Model\NotificationContentModel;

// Get an instance of the NotificationManager provided by this bundle.
// Using the service, that is available since the bundle installation.
// Better would be to inject the service as a dependency in your service configuration.
$notificationManager = $this->get('sc_expo_notifications.notification_manager');

$data = ['foo' => 'bar'];

// Create a NotificationContentModel
$notificationContentModel = new NotificationContentModel();
$notificationContentModel
    ->setTo('H-Dsb2ATt2FHoD_5rVG5rh')
    ->setBody('test message')
    ->setData($data)
    ->setPriority('low');

// Create a second NotificationContentModel
$anotherNotificationContentModel = new NotificationContentModel();
$anotherNotificationContentModel
    ->setTo('Z-5sb2AFt2FHoD_5rVG5rh')
    ->setBody('Your message here')
    ->setData($data)
    ->setPriority('medium');

$notificationContentModels = [
    $notificationContentModel,
    $anotherNotificationContentModel,
];

// Send the notifications.
$httpResponse = $notificationManager->sendNotificationsHttp($notificationContentModels);

// Handle the response using the notificationManager. Enriches the NotifcationContentModel with the http response data.
$notificationContentModels = $notificationManager->handleHttpResponse($httpResponse, $notificationContentModels);

// The notificationContentModels have now been updated. The info for each notification is now stored in each model.

故障排除

如果由于某种原因,服务sc_expo_notifications.notification_manager不可用,请使用bin/console debug:container | grep notification调试您的容器。您应该看到

sc_expo_notifications.guzzle_client                GuzzleHttp\Client
sc_expo_notifications.notification_manager         Solvecrew\ExpoNotificationsBundle\Manager\NotificationManager

第一个服务是guzzle客户端,它是我们包的依赖项。第二个服务是提供给我们包处理所有通知相关任务的notificationManager。

基于Expo推送通知API

要查看过程和Expo推送通知服务的API文档,请参阅:https://docs.expo.io/versions/v14.0.0/guides/push-notifications.html

许可证

由SolveCrew于2017年创建。如果您喜欢,请联系我们:[info@solvecrew.com](mailto:info@solvecrew.com)或访问我们的网站:[www.solvecrew.com](http://www.solvecrew.com) MIT