项目作者: al-one

项目描述 :
XiaoMi Push For Laravel Notifications
高级语言: PHP
项目地址: git://github.com/al-one/laravel-xiaomi-push.git
创建时间: 2019-09-01T09:32:53Z
项目社区:https://github.com/al-one/laravel-xiaomi-push

开源协议:MIT License

下载


XiaoMi Push For Laravel Notifications

Installing

  1. # composer.json
  2. "minimum-stability": "dev",
  3. "prefer-stable": true,
  1. $ composer require "al-one/laravel-xiaomi-push" -vvv

Config

  1. # optional if >= 5.5
  2. # config/app.php
  3. <?php
  4. return [
  5. 'providers' => [
  6. Alone\LaravelXiaomiPush\ServiceProvider::class,
  7. ],
  8. ];
  1. # config/services.php
  2. [
  3. 'xiaomi_push' => [
  4. 'send_by' => 'account',
  5. 'android' => [
  6. 'bundle_id' => 'com.app.bundle_id',
  7. 'appid' => '1234567890123456',
  8. 'key' => '1234567890123456',
  9. 'secret' => 'abcdefghijklmn==',
  10. ],
  11. 'ios' => [
  12. 'bundle_id' => 'com.app.bundle_id',
  13. 'appid' => '1234567890123456',
  14. 'key' => '1234567890123456',
  15. 'secret' => 'abcdefghijklmn==',
  16. 'sandbox' => false,
  17. ],
  18. ],
  19. ];

Usage

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Notifications\Notifiable;
  5. class User extends Model
  6. {
  7. use Notifiable;
  8. /**
  9. * 小米推送路由
  10. */
  11. public function routeNotificationForXiaomiPush()
  12. {
  13. // return $this->xiaomi_push_regid;
  14. return $this->getKey();
  15. }
  16. /**
  17. * 如果不同用户所属的APP包名可能不同,请添加此方法
  18. */
  19. public function getAppPackage()
  20. {
  21. return 'com.app.bundle_id';
  22. }
  23. /**
  24. * 添加此方法以判断用户是否为苹果设备用户
  25. */
  26. public function isIosDevice()
  27. {
  28. return true;
  29. }
  30. }
  1. <?php
  2. use Illuminate\Support\Facades\Notification;
  3. use Alone\LaravelXiaomiPush\XiaomiNotification;
  4. $msg = (new XiaomiNotification)
  5. ->title('通知标题')
  6. ->description('通知描述')
  7. ->body('通知描述 For iOS')
  8. ->payload([
  9. 'action' => 'openApp',
  10. ])
  11. ->setHandler(function($msg,$notifiable,$cfg,$type = null)
  12. {
  13. /**
  14. * @link https://github.com/al-one/xmpush-php/blob/master/sdk/xmpush/Builder.php
  15. * @link https://github.com/al-one/xmpush-php/blob/master/sdk/xmpush/IOSBuilder.php
  16. */
  17. if($msg instanceof \xmpush\IOSBuilder)
  18. {
  19. $msg->badge(1);
  20. }
  21. elseif($msg instanceof \xmpush\Builder)
  22. {
  23. $msg->notifyId(rand(0,4));
  24. }
  25. return $msg;
  26. });
  27. $user->notify($msg);
  28. Notification::send($users,$msg);

License

MIT