netandreus/expo-notifications-bundle

用于处理expo react-native框架推送通知服务后端任务的包。支持Symfony 4.4+

安装: 43

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 17

类型:symfony-bundle

1.2.1 2020-09-24 14:14 UTC

This package is auto-updated.

Last update: 2024-09-10 22:48:30 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

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

用法

此包提供了一个简单的方式来使用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[]的包围)。消息数组中的第一个消息将发送到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.

如果您的用例更复杂,或者您想利用更多通知功能,可以使用通知管理器的 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 或访问我们的网站:www.solvecrew.com MIT