Created
October 3, 2021 13:06
-
-
Save hogivano/b1a6c129aca13d47296447c745cd890a to your computer and use it in GitHub Desktop.
Sampel Push Notification
This file contains hidden or 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
//push notificaion | |
public function push($req){ | |
$get = ResourceNotification::where('user_id', $req->receiver_id)->with('userId')->get(); | |
$tokens = []; | |
foreach ($get as $g) { | |
// code... | |
array_push($tokens, $g->resource); | |
} | |
$limitToken = array_chunk($tokens, 500); | |
// dd($limitToken); | |
if (sizeof($tokens) > 0){ | |
foreach ($limitToken as $t) { | |
// code... | |
$optionBuilder = new OptionsBuilder(); | |
$optionBuilder->setTimeToLive(60*20); | |
$notificationBuilder = new PayloadNotificationBuilder($req->title); | |
$notificationBuilder->setBody($req->message) | |
->setSound('default'); | |
$dataBuilder = new PayloadDataBuilder(); | |
$dataBuilder->addData(json_decode(json_encode($req), true)); | |
$option = $optionBuilder->build(); | |
$notification = $notificationBuilder->build(); | |
$data = $dataBuilder->build(); | |
$downstreamResponse = FCM::sendToGroup($t, $option, $notification, $data); | |
$downstreamResponse->numberSuccess(); | |
$downstreamResponse->numberFailure(); | |
$downstreamResponse->tokensFailed(); | |
// dd($downstreamResponse); | |
print('notification status ' . $downstreamResponse->numberSuccess()); | |
} | |
} else { | |
//print("tidak ada token yang ditemukan"); | |
} | |
} | |
public function pushPublicToken($req){ | |
$tokens = []; | |
$get = ResourceNotification::where('user_id', -1)->get(); | |
foreach ($get as $g) { | |
// code... | |
array_push($tokens, $g->resource); | |
} | |
$limitToken = array_chunk($tokens, 500); | |
if (sizeof($tokens) > 0){ | |
foreach ($limitToken as $t) { | |
// code... | |
$optionBuilder = new OptionsBuilder(); | |
$optionBuilder->setTimeToLive(60*20); | |
$notificationBuilder = new PayloadNotificationBuilder($req->title); | |
$notificationBuilder->setBody($req->message) | |
->setSound('default'); | |
$dataBuilder = new PayloadDataBuilder(); | |
$dataBuilder->addData(json_decode(json_encode($req), true)); | |
$option = $optionBuilder->build(); | |
$notification = $notificationBuilder->build(); | |
$data = $dataBuilder->build(); | |
$downstreamResponse = FCM::sendToGroup($t, $option, $notification, $data); | |
$downstreamResponse->numberSuccess(); | |
$downstreamResponse->numberFailure(); | |
$downstreamResponse->tokensFailed(); | |
// dd($downstreamResponse); | |
print('notification status ' . $downstreamResponse->numberSuccess()); | |
} | |
} else { | |
//print("tidak ada token yang ditemukan"); | |
} | |
} | |
public function pushAllUser($req){ | |
$user = User::all(); | |
foreach ($user as $u) { | |
// code... | |
if (Auth::user()->id !== $u->id){ | |
$new = new Notification(); | |
$new->sender_id = Auth::user()->id; | |
$new->receiver_id = $u->id; | |
$new->features_id = $req->features_id; | |
$new->notification_type = $req->notification_type; | |
$new->link_url = $req->link_url; | |
$new->title = $req->title; | |
$new->picture_url = $req->picture_url; | |
$new->message = $req->message; | |
$new->save(); | |
$this->push($new); | |
} | |
} | |
$this->pushPublicToken($req); | |
} | |
public function pushMultipleUser($req){ | |
foreach ($req->receiver_id as $r) { | |
// code... | |
$new = new Notification(); | |
$new->sender_id = Auth::user()->id; | |
$new->receiver_id = $r; | |
$new->features_id = $req->features_id; | |
$new->notification_type = $req->notification_type; | |
$new->link_url = $req->link_url; | |
$new->title = $req->title; | |
$new->picture_url = $req->picture_url; | |
$new->message = $req->message; | |
$new->save(); | |
$this->push($new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment