espiru / rms-push-notifications-bundle

为移动设备推送通知/消息

安装: 45

依赖项: 0

建议者: 0

安全: 0

星级: 0

观察者: 4

分支: 152

类型:symfony-bundle

0.2.7 2019-11-14 08:11 UTC

README

一个允许向移动设备发送推送通知的包。目前支持Android(C2DM、GCM)、Blackberry和iOS设备。

安装

要在您的Symfony2项目中使用此包,请将以下内容添加到您的composer.json

{
    "require": {
        // ...
        "richsage/rms-push-notifications-bundle": "dev-master"
    }
}

并在您的kernel中启用它

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new RMS\PushNotificationsBundle\RMSPushNotificationsBundle(),
    );
}

注意:如果您仍在使用Symfony 2.0,请使用symfony2.0分支。

配置

可用的配置选项如下。请注意,只有当您为特定服务提供了相应的配置时,这些服务才会可用。

rms_push_notifications:
  android:
      timeout: 5 # Seconds to wait for connection timeout, default is 5
      c2dm:
          username: <string_android_c2dm_username>
          password: <string_android_c2dm_password>
          source: <string_android_c2dm_source>
      gcm:
          api_key: <string_android_gcm_api_key> # This is titled "Server Key" when creating it
          use_multi_curl: <boolean_android_gcm_use_multi_curl> # default is true
          dry_run: <bool_use_gcm_dry_run>
  ios:
      timeout: 60 # Seconds to wait for connection timeout, default is 60
      sandbox: <bool_use_apns_sandbox>
      pem: <path_apns_certificate> # can be absolute or relative path (from app directory)
      passphrase: <string_apns_certificate_passphrase>
  mac:
      timeout: 60 # Seconds to wait for connection timeout, default is 60
      sandbox: <bool_use_apns_sandbox>
      pem: <path_apns_certificate>
      passphrase: <string_apns_certificate_passphrase>
  blackberry:
      timeout: 5 # Seconds to wait for connection timeout, default is 5
      evaluation: <bool_bb_evaluation_mode>
      app_id: <string_bb_app_id>
      password: <string_bb_password>
  windowsphone:
      timeout: 5 # Seconds to wait for connection timeout, default is 5

注意:如果您使用Windows,您可能需要将Android GCM的use_multi_curl标志设置为false,以确保GCM消息能够正确发送。

超时默认值是在引入此配置值之前默认值。

使用方法

以下是一个将第一条消息推送到iOS设备的示例,我们假设您已经正确设置了配置

use RMS\PushNotificationsBundle\Message\iOSMessage;

class PushDemoController extends Controller
{
    public function pushAction()
    {
        $message = new iOSMessage();
        $message->setMessage('Oh my! A push notification!');
        $message->setDeviceIdentifier('test012fasdf482asdfd63f6d7bc6d4293aedd5fb448fe505eb4asdfef8595a7');

        $this->container->get('rms_push_notifications')->send($message);

        return new Response('Push notification send!');
    }
}

发送方法会检测消息类型,如果您传递一个AndroidMessage,它将自动通过C2DM/GCM服务器发送,对Mac和Blackberry也是如此。

Android消息

由于C2DM和GCM仍然可用,AndroidMessage类有一个小的标志可以切换要发送的服务。如下所示使用

use RMS\PushNotificationsBundle\Message\AndroidMessage;

$message = new AndroidMessage();
$message->setGCM(true);

以GCM消息的形式发送而不是C2DM。

iOS反馈服务

Apple推送通知服务还公开了一个反馈服务,您可以从中获取有关失败的推送通知的信息 - 更多详情请参阅此处

此服务在包内可用。以下代码演示了如何从服务中检索数据

$feedbackService = $container->get("rms_push_notifications.ios.feedback");
$uuids = $feedbackService->getDeviceUUIDs();

在这里,$uuids包含一个包含时间戳、令牌长度和设备UUID的Feedback对象的数组。

Apple建议您每天轮询此服务。

Windows Phone - Toast支持

该包对Windows Phone有beta支持,并支持Toast通知。使用WindowsphoneMessage消息类进行相应发送。

感谢

首先,感谢所有为此包做出贡献的贡献者!

其次,感谢JetBrains为本项目赞助了一个开源的PhpStorm许可证。