Created
July 10, 2017 06:29
-
-
Save ayatmaulana/1a2788e0a2b58967fb5295163d15b505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin\Notification; | |
use LaravelFCM\Message\OptionsBuilder; | |
use LaravelFCM\Message\PayloadDataBuilder; | |
use LaravelFCM\Message\PayloadNotificationBuilder; | |
use FCM; | |
use \App\FcmToken; | |
use Illuminate\Http\Request; | |
use App\Http\Controllers\Controller; | |
class FirebaseNotification extends Controller | |
{ | |
protected static $payload; | |
protected static $data; | |
protected static $token; | |
public function __construct() | |
{ | |
} | |
public static function testPushNotif(Request $request){ | |
$data = $request->all(); | |
self::$token = $data['fcm_token']; | |
self::$data = ['type' => 'test', 'data' => 'data test']; | |
self::$payload = null; | |
// try { | |
dd(self::sendMessage()); | |
//echo 'success send to '. $data['fcm_token']; | |
// } catch(\Exception $e){ | |
// echo 'fail send to ' . $data['fcm_token']; | |
// } | |
} | |
public static function pushNotifPertanyaan($payload = [], $data = [], $type = 'pertanyaan', $user_id) | |
{ | |
self::$payload = $payload; | |
self::$data = [ | |
'type' => 'pertanyaan', | |
'data' => $data, | |
]; | |
self::$token = self::getTokenById($user_id); | |
self::sendMessage(); | |
} | |
public static function pushNotif($payload = [], $data = [], $type = 'episode') | |
{ | |
#delete some params | |
unset($data['data']->is_notification_sended); | |
unset($data['data']->active); | |
unset($data['data']->cron); | |
self::$payload = $payload; | |
self::$data = [ | |
'type' => $type, | |
'data' => $data['data'] | |
]; | |
self::$token = self::getAllToken(); | |
self::sendMessage(); | |
} | |
public static function sendMessage() | |
{ | |
$dataToSend = self::$data; | |
$payloadToSend = self::$payload; | |
try { | |
$optionBuiler = new OptionsBuilder(); | |
$optionBuiler->setTimeToLive(60*20); | |
$notificationBuilder = new PayloadNotificationBuilder($payloadToSend['title']); | |
$notificationBuilder->setBody($payloadToSend['body']) | |
->setSound((empty($payloadToSend['sound']) ? 'default' : $payloadToSend['sound'])); | |
$dataBuilder = new PayloadDataBuilder(); | |
$dataBuilder->addData($dataToSend); | |
$option = $optionBuiler->build(); | |
$notification = $notificationBuilder->build(); | |
$data = $dataBuilder->build(); | |
// You must change it to get your tokens | |
$tokens = self::$token; | |
#broadcast to Firebase | |
$downstreamResponse = FCM::sendTo($tokens, $option, null, $data); | |
return $downstreamResponse; | |
} catch(\Exception $e){ | |
} | |
// dd($downstreamResponse->numberSuccess()); | |
// dd($downstreamResponse->numberFailure()); | |
// $downstreamResponse->numberModification(); | |
//return Array - you must remove all this tokens in your database | |
// $downstreamResponse->tokensToDelete(); | |
//return Array (key : oldToken, value : new token - you must change the token in your database ) | |
// dd($downstreamResponse->tokensToModify()); | |
//return Array - you should try to resend the message to the tokens in the array | |
// dd($downstreamResponse->tokensToRetry()); | |
// return Array (key:token, value:errror) - in production you should remove from your database the tokens present in this array | |
// dd($downstreamResponse->tokensWithError()); | |
} | |
public static function getAllToken() | |
{ | |
$tokens = FcmToken::where('episode_enabled','=',1)->pluck('fcm_token')->toArray(); | |
return $tokens; | |
} | |
public static function getTokenById($id) | |
{ | |
$tokens = FcmToken::where('user_id',$id)->where('tanya_enabled','=',1)->pluck('fcm_token')->toArray(); | |
return $tokens; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment