netandreus/expo-notifications-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

netandreus/expo-notifications-bundle

Composer 安装命令:

composer require netandreus/expo-notifications-bundle

包简介

Bundle to handle the BE tasks of the push notifications service for the expo react-native Framework. Fixes for support Symfony 4.4+

README 文档

README

This Bundles purpose is to handle the BE tasks of the push notifications service for the expo react-native Framework.

Installation

Install the bundle using composer:

composer require solvecrew/expo-notifications-bundle

Enable the Bundle in the app/AppKernel.php file:

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

Configuration

At the moment, this bundle only has a single optional configuration parameter.

If you want (optional), add this to your app/config/config.yml file:

sc_expo_notifications:
    expo_api_endpoint: '%expo_api_endpoint%'

And then add the expo_api_endpoint parameter in your app/config/parameters.yml file:

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

If you prefer to not add it as a parameter in your parameters.yml file you can add the URI in your config.yml file directly:

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

IMPORTANT: All this is completely OPTIONAL. If you don't add the config at all, it will use https://exp.host/--/api/v2/push/send as fallback since it is the endpoint from the official Expo Documentation.

Usage

This bundle provides you with an easy way to send push notifications for a front-end application using the Expo React-Native framework. Therefore the bundle provides you with several helpful things:

  • NotificationContentModel: A model representing the requestdata for a single notification. As specified by the Expo API.
  • NotificationManager: A Manager to handle the preparing of the notification, the sending and the reponse.

The service of the NotificationManager is sc_expo_notifications.notification_manager.

  • Use it in a controller with $this->container->get('sc_expo_notifications.notification_manager').
  • Inject it as a dependency like:
app.example_manager:
    class: AcmeBundle\Manager\ExampleManager
    arguments: ['@sc_expo_notifications.notification_manager']

NOTE that the important part here is the arguments: ['@sc_expo_notifications.notification_manager'] of course.

After you have the NotificationManager available you can access its functions. Popular functions are:

  1. sendNotifications(...): Send multiple notifications in one API request.
    /**
     * 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
    {
		...
    }

Therefore you need to provide an array of messages as strings and an array of tokens as strings (to be more specific: The recipients ExponentPushToken. Like sITGtlHf1-mSgUyQIVbVMJ, without the ExponentPushToken[] sourrounding.). The first message in the messages array will be delivered to the first token (recipient) in the tokens array. And so on. Optionally you can provide a titles array which holds titles for the notifications. Last, you can provide an array of data arrays that will be added to the notification as a JSON object for further handling in the front-end. It is important to know, that each notification needs an array as data! See the Full Example below for more information.

The function returns you an array of NotificationContentModel. One for each notification that was tried to send. Those NotificationContentModels hold all the information about the notification.

For example:

  • to: The token that represents the recipient.
  • title: The title, if provided.
  • body: The actual message of the notification.
  • wasSuccessful: A boolean indicating whether the notification was send (does NOT mean it was recieved or viewed).
  • responseMessage: A message that was returned by the Expo API on unsuccessful request for the specific notification.
  • responseDetails: An array holding error specific information.
  1. sendNotification(...): Send a single notification providing only a message string and a token. Optionally a title.
    /**
     * 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
    {
		...
    }

As you can see, this one is really straight forward. It returns a single NotificationContentModel as descibed above. The title (string) and the data (array) are optional. If provided, $data must be an array.

Full Example

To even ease the integration process further, see the following example.

// 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.

If your use case is more complex or you just want to leverage more of the notification funtions you can use the sendNotificationHttp function of the NotificationManager. For that you need to create the NotificationContentModel yourself.

// 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]);

If you want to send multiple notifications this way, use sendNotificationsHttp (plural).

// 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.

Troubleshooting

If the service sc_expo_notifications.notification_manager is not available for some reason, debug your container with bin/console debug:container | grep notification. You should see:

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

The first service is the guzzle client which is the dependency of our bundle. The second service is the notificationManager the bundle provides to handle all notification related tasks.

Based on the Expo push notifications API

To see the process and the API documentation for the Expo push notifications service see: https://docs.expo.io/versions/v14.0.0/guides/push-notifications.html

LICENSE

Created by SolveCrew 2017. Contact us if you like: info@solvecrew.com or visit our website: www.solvecrew.com MIT

netandreus/expo-notifications-bundle 适用场景与选型建议

netandreus/expo-notifications-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「push notifications」 「expo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 netandreus/expo-notifications-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 netandreus/expo-notifications-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 netandreus/expo-notifications-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 44
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-24