- 授权协议: MIT
- 开发语言: PHP
- 操作系统: 跨平台
- 软件首页: https://github.com/medz/laravel-jpush-notification-channel
- 软件文档: https://github.com/medz/laravel-jpush-notification-channel#readme
- 官方下载: https://github.com/medz/laravel-jpush-notification-channel
软件介绍
我们在开发针对国内运营的时候进行需要使用过程的几家推送,极光推送则是其中之一。这个包就可以让你方便地在你构建的 Laravel 应用中进行极光推送的使用。
前提
- PHP >= 7
- Laravel >= 5.5
安装
在 Laravel 应用目录使用 Composer 进行依赖:
composer require medz/laravel-jpush-notification-channel
包中依赖了匹配的 jpush/jpush 依赖版本为 ^3.6,你已经依赖了更低版本的不兼容版本包,使用的时候要小心了!
配置
在 config/services.php 中进行如下配置:
return [
'jpush' => [
'app_key' => env('JPUSH_APP_KEY', ''),
'master_secret' => env('JPUSH_MASTER_SECRET', ''),
'apns_production' => env('JPUSH_APNS_PRODUCTION', false),
],
];
然后在 .env 文件中进行配置:
JPUSH_APP_KEY=
JPUSH_MASTER_SECRET=
JPUSH_APNS_PRODUCTION=
使用
首先,要在数据模型上添加一个 routeNotificationForJpush 方法:
use Illuminate\Foundation\Auth\User as Authenticatable;
use Medz\Laravel\Notifications\JPush\Sender as JPushSender;
class User extends Authenticatable
{
/**
* Get Notification for JPush sender.
* @return \Medz\Laravel\Notifications\JPush\Sender
*/
protected function routeNotificationForJpush()
{
return new JPushSender([
'platform' => 'all',
'audience' => [
'alias' => sprintf('user_%d', $this->id),
],
]);
}
}
这里我们返回一个 Medz\Laravel\Notifications\JPush\Sender 实例,可以使用构造参数快速配置,如同上面一样,也可以使用链式调用进行配置。链式调用的 API 如下:
setPlatform设置平台,值有 all、winphone、android 和 iossetAudience推送目标进行设置
setAudience 方法或者构造参数中的 audience 设置参考:推送目标文档。
然后打开通知类,添加一个toJpush 方法,这里我们已app/Notifications/CommentNotification.php 为例:
<?php
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use Medz\Laravel\Notifications\JPush\Message as JPushMessage;
class CommentNotification extends Notification
{
public function toJpush($notifiable)
{
$message = new JPushMessage();
// TODO
/*
====== 把所有的配置都进行配置 ===
$message->setAlert('Alert.'); // 简单地给所有平台推送相同的 alert 消息
// 自定义消息
$message->setMessage('Message', [
'title' => '', // 通知标题,会填充到 toast 类型 text1 字段上
'_open_page' => '', 点击打开的页面名称
'extras' => [], // 自定义的数据内容
]);
// iOS 通知
$message->setNotification(JPushMessage::IOS, 'Alert 内容', [
'alert' => '', // 覆盖第二个参数的 Alert 内,推荐不传,
'sound' => '', // 表示通知提示声音,默认填充为空字符串
'badge' => '', // 表示应用角标,把角标数字改为指定的数字;为 0 表示清除,支持 '+1','-1' 这样的字符串,表示在原有的 badge 基础上进行增减,默认填充为 '+1'
/// ...
])
// 更多通知请参考 https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification 官方文档
// 使用 `setNotification` 方法第一个常量有三个: IOS/ANDROID/WP
// 可选参数
$message->setOptions([]); // 参考 https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#options
*/
return $message;
}
}
toJpush方法需要返回一个Medz\Laravel\Notifications\JPush\Message对象实例!
完成上面的配置后,就可以推送了,记得在 via 方法中返回 jpush 这个值哈,例如:
public function via()
{
return ['database', 'jpush'];
}
PHP and MySQL Web Development
Luke Welling、Laura Thomson / Sams / July 25, 2007 / $49.99
Book Description PHP and MySQL Web Development teaches you to develop dynamic, secure, commerical Web sites. Using the same accessible, popular teaching style of the three previous editions, this b......一起来看看 《PHP and MySQL Web Development》 这本书的介绍吧!
