developerdynamo/laravel-push-notification

此包已被弃用且不再维护。未建议替代包。

Laravel包,用于跨所有平台发送推送通知

此包尚未发布版本,信息有限。


README

有关介绍,请参阅此文章: Developer Dynamo PushNotification包

有许多PHP库可以帮助您发送推送通知,但据我所知,没有库可以轻松创建和管理完整的推送通知服务。

使用此包,您可以创建针对可能发生在您的用户身上的任何相关事件的负载集合,使用Eloquent在您的数据库中过滤设备列表,并使用一行代码从一到数百万条消息中发送消息。

###当前支持的平台(桌面和移动)

  • iOS(仅移动应用)
  • Android
  • Chrome, Firefox

##安装 将以下行添加到您的 composer.json 文件中的 "require" 部分

"developerdynamo/laravel-push-notification": "0.*"

更新 composer,使用以下命令

"composer update"

###提供者和外观 与所有提供者一样,将以下行添加到您的 config/app.php 中

'providers' => [
	...
	DeveloperDynamo\PushNotification\PushNotificationProvider::class,
],
'aliases' => [
	...
	'NotificationBridge' => DeveloperDynamo\PushNotification\Facades\PushNotificationBridge::class,
],

###配置 最后,您需要为此包生成一个配置文件。运行以下 composer 命令

php artisan vendor:publish --provider="DeveloperDynamo\PushNotification\PushNotificationProvider"

此命令将在您的配置目录中生成 pushnotification.php 文件。

return [

    "ios" => [
    	/*
    	 * A valid PEM certificate generated from Apple Push Service certificate
    	 */
        "certificate" 	=> storage_path('app')."/aps.pem",
    		
    	/*
    	 * Password used to generate a certificate
    	 */
        "passPhrase"  	=> ""
    ],
	
    "android" => [
    	/*
    	 * Google GCM api key
    	 * You can retrieve your key in Google Developer Console
    	 */
        "apiKey"      	=> "",
    ]

];

请记住添加您的 GCM API 密钥和 PEM 证书路径。

##令牌 您应该有一个模型来存储设备信息到您的数据库中。为了使您的模型能够直接从 PushNotification 包中使用,您只需添加 use TokenTrait

use DeveloperDynamo\PushNotification\TokenTrait;

class YourDeviceTokenTable extends Model
{
    use TokenTrait;
}

为了让 PushNotification 包自动工作,您的表需要两个列,一个包含平台名称,另一个包含设备令牌。

默认情况下,这两个列的名称被认为是 "platform""device_token"

+--------------+
| Field        |
+--------------+
| id           |
| platform     | enum (android, ios, web)
| device_token |
| created_at   |
| updated_at   |
+--------------+

如果您的数据库表使用了不同的名称,您可以根据表的结构自定义它们。例如,您的表中有名为 "os" 的平台列而不是 "platform",用于存储设备令牌的列名为 "token" 而不是 "device_token"

+--------------+
| Field        |
+--------------+
| id           |
| os           | (instead of "platform")
| token        | (instead of "device_token")
| created_at   |
| updated_at   |
+--------------+

您可以使用 $columnName 属性覆盖包中使用的标准名称

class YourDeviceTokenTable extends Model
{
    use TokenTrait;
    
    /**
	 * Column name into DB table to store device informations
	 * 
	 * @var array
	 */
	protected $columnName = [
			"platform" => "os",
			"device_token" => "token",
	];
}

您可以设计您的表,但只有这两个字段是必须的才能与 PushNotification 包一起工作。

##负载 您只需创建一个实现 DeveloperDynamo\PushNotification\Contracts\Payload 的类,并用您的负载内容覆盖 apsPayloadgcmPayload 属性。

namespace App\Payload;

use App\User;
use DeveloperDynamo\PushNotification\Contracts\Payload;

class AddPhotoPayload extends Payload
{
	/**
	 * Generate Notification Payload
	 *
	 * @param User $user
	 * @return void
	 */
	public function __construct(User user)
	{
		//IOS payload format	
		$this->apsPayload = [
				"alert" => [
					"title" => $user->first_name." posted a photo",
					"body" 	=> $user->first_name." added a new photo in her gallery",
				],
		];
		
		//Android payload format
		$this->gcmPayload = [
				"title" 	=> $user->first_name." posted a photo",
				"message" 	=> $user->first_name." added a new photo in her gallery",
		];
	}
}

您可以为要发送给用户的每个事件或消息创建负载集合。

##发送 现在,您可以从数据库中获取设备令牌列表,并且有一个特定事件的负载。要向设备列表发送负载,您可以使用从 Payload 类继承的 send 方法。

###常规发送

//Create payload
$payload = new AddPhotoPayload(User::findOrFail(1));

//Retrieve devices list with your own criteria
$tokens = YourDeviceTokenTable::all();

//send directly
$payload->send($tokens);

###通过队列 包使用队列在后台执行发送任务,以原生方式发送通知。您需要在 config/queue.php 中设置您的队列提供者,并且您的负载将发送到队列。

您可以使用 "send" 方法的第二个参数在特定队列中安排作业。

//push in queue
$payload->send($payload, $tokens, "your-queue-name");

###桌面通知 感谢 GCM 服务,您可以发送通知到桌面浏览器,Chrome 和 Firefox 都受支持,因为这两个浏览器实现了 GCM 客户端。