jgab-net / android-gcm
使用GCM发送推送通知
1.0.0
2014-04-10 01:03 UTC
Requires
- php: >=5.3.0
- illuminate/database: 4.1.*
- illuminate/support: 4.1.*
This package is not auto-updated.
Last update: 2024-09-28 16:20:33 UTC
README
laravel包,用于通过Google Cloud Message (GCM)发送通知
https://developer.android.com.cn/google/gcm/gcm.html
安装
composer require jgab-net/android-gcm dev-master
发布配置
php artisan config:publish jgab-net/android-gcm
配置文件将发布到app/config/packages/jgab-net/android-gcm/config.php
将生成的api_key放置在 https://cloud.google.com/console 上的服务器
return array( 'api_key' => 'aquí el api_key' );
配置数据库
需要运行包的迁移以生成存储设备token(registrations_id)的表
php artisan migrate --package=jgab-net/android-gcm
非常重要,这个迁移应该在运行了项目迁移或系统已有users表后执行,因为会创建一个外键与users.id相关联,如果不存在该表,迁移将显示错误,但是你可以忽略错误继续,只是会丢失外键
编程
要存储token(registration_id),只需添加以下行到所需位置(假设registration_id来自Android设备,而user_id属于登录应用程序的用户)
AndroidGcm::addRegistrationId($registration_id, $user_id);
如果你使用其他包来管理用户权限,可以使用过滤器after,例如:
- filters.php
Route::filter('android.gcm',function($route, $request, $response){ /*verificamos que venga de una respuesta json (Response::json()) y que exista un registration_id en el input */ if($response instanceof \Illuminate\Http\JsonResponse && Input::has('registration_id')){ // Obtenemos el contenido de la respuesta $content = json_decode($response->getContent()); AndroidGcm::addRegistrationId(Input::get('registration_id'), $content->user->id); } });
详细说明已在示例代码中提供
- routes.php
Route::post('auth', array('after' => 'android.gcm', 'uses' => 'Vendor\Paquete\Controller@method'));
通知
要发送通知,只需执行send方法,第一个参数是包含要通知的设备token(registration_ids)的数组,第二个参数是接收实际通知token(registration_ids)的回调函数
库内部将替换过时的token(registration_ids),以便在下次发送时将通知传输到缺少的设备
AndroidGcm::send($registrationIds,function($successRegistrationIds){ /* Aquí puedes actualizar la bandera que indique que ya no debes enviar la notificación a los dispositivos Puedes usar $successRegistrationIds para valerte de esto. */ });
如果你需要在回调函数中传递其他值,可以使用(use)
AndroidGcm::send($registrationIds,function($successRegistrationIds) use($otherValue1,$otherValue2){ /* */ });
获取GCM的真实响应
AndroidGcm::getApiResponse()