potagercity / expo-notifications-bundle
用于处理 expo react-native 框架推送通知服务后端任务的组件包。源自 solvecrew one 的分支
Requires
- php: >=7.0
- eightpoints/guzzle-bundle: ^6.0|~7.0
- eightpoints/guzzle-wsse-middleware: ~4.0
- guzzlehttp/guzzle: ~6.0
- psr/log: ~1.0
- symfony/dependency-injection: ~2.3|~3.0|~4.0
- symfony/event-dispatcher: ~2.3|~3.0|~4.0
- symfony/expression-language: ~2.3|~3.0|~4.0
- symfony/http-kernel: ~2.3|~3.0|~4.0
Requires (Dev)
- php: >=5.3
- eightpoints/guzzle-bundle: ^6.0|~7.0
- phpunit/phpunit: ~5.4
- symfony/config: 2.3|~3.0
This package is not auto-updated.
Last update: 2024-09-28 08:01:16 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
如果您不想将 URI 添加到 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:一个用于处理通知准备、发送和响应的 Manager。
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 后,可以访问其功能。常用的功能包括
- 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:包含错误特定信息的数组。
- 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
第一个服务是我们bundle的依赖项guzzle客户端。第二个服务是bundle提供的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