dytrof / push-notification-bundle
用于通过OneSignal发送推送通知的Symfony2扩展包
0.2.2
2017-08-25 17:46 UTC
Requires
- php: >=5.6
- norkunas/onesignal-php-api: 1.0.x-dev
- php-http/guzzle6-adapter: ^1.1
- symfony/framework-bundle: ^2.8
- twig/twig: 1.*
Requires (Dev)
- phpunit/phpunit: 5.*
README
用于通过OneSignal发送推送通知的Symfony2扩展包
安装
步骤1: 使用Composer require
$ php composer.phar require "norkunas/onesignal-php-api":"1.0.x-dev" "dmytrof/push-notification-bundle"
步骤2: 在kernel中启用扩展包
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Dmytrof\PushNotificationBundle\DmytrofPushNotificationBundle(),
// ...
);
}
配置
在config.yml中启用扩展包
# app/config/config.yml
dmytrof_push_notification:
provider: one_signal
one_signal: # See: https://documentation.onesignal.com/docs/web-push-sdk-setup-http
app_id: "%one_signal.app_id%"
app_auth_key: "%one_signal.auth_key%"
safari_web_id: "%one_signal.safari_web_id%" # Safari Support (Optional). See: https://documentation.onesignal.com/docs/web-push-sdk-setup-http#section-3-safari-support-optional-
subdomain: "%one_signal.subdomain%" # HTTP Setup ONLY. See https://documentation.onesignal.com/docs/web-push-sdk-setup-http#section-1-4-choose-subdomain
使用方法
Web Push SDK配置
{# layout.html.twig #}
<head>
{# ... #}
{{ dmytrof_push_notification_web_sdk() }}
</head>
标记用户
<?php
// SomeController
public function addTagAction()
{
// ...
$this->get('dmytrof_push_notification.provider')->addTag('tagName', 'tagValue');
// ...
}
public function addTagsAction()
{
// ...
$this->get('dmytrof_push_notification.provider')->addTags([
'tagName1' => 'value1',
'tagName2' => 'value2'
]);
// ...
}
用户将在使用Web Push SDK渲染布局后进行标记
移除标签
<?php
// SomeController
public function removeTagsAction()
{
// ...
$this->get('dmytrof_push_notification.provider')->removeTags([
'tagName1',
'tagName2'
]);
// ...
}
标签将在使用Web Push SDK渲染布局后移除
发送推送通知
<?php
// SomeController
public function sendNotificationAction()
{
// Send to all
$provider = $this->get('dmytrof_push_notification.provider');
$notification = $provider->createNotification()
->setSubject('Notification Subject')
->setMessage('Test notification for all')
->includeSegments(['All']);
$provider->sendNotification($notification);
// Filter by tags
$notification = $provider->createNotification()
->setSubject('Notification Subject')
->setMessage('Filtered by tags')
->filterByTag('tagName1', '=', 'value1')
->filterByTag('tagName2', '=', 'value2', true);
$provider->sendNotification($notification);
// Send templated message to user
$notification = $provider->createNotification()
->setTemplate('AppBundle:PushNotification:test_notification.html.twig', [
'user' => $user
])
->filterByTag('userId', '=', $user->getId());
$provider->sendNotification($notification);
}
配置消息模板
{# 'AppBundle:PushNotification:test_notification.html.twig' #}
{% extends 'DmytrofPushNotificationBundle:PushNotification:layout.html.twig' %}
{% block Subject %}
Test templated subject
{% endblock %}
{% block Message %}
Your user ID: {{ user.id }}
{% endblock %}
{% block Url %}
{{ absolute_url(path('YOUR_ROUTE')) }}
{% endblock %}