33namespace Webkul \PWA \Http \Controllers ;
44
55use Illuminate \Http \Request ;
6- use Webkul \PWA \Models \PushNotification as PushNotification ;
76use Webkul \PWA \Repositories \PushNotificationRepository as PushNotificationRepository ;
8- use LaravelFCM \Message \OptionsBuilder ;
9- use LaravelFCM \Message \PayloadDataBuilder ;
10- use LaravelFCM \Message \PayloadNotificationBuilder ;
11- use FCM ;
12- use DB ;
137
8+ /**
9+ * Push Notification controller
10+ *
11+ * @author Vivek Sharma <[email protected] > 12+ * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
13+ */
1414class PushNotificationController extends Controller
1515{
16+ /**
17+ * Display a listing of the resource.
18+ *
19+ * @return \Illuminate\Http\Response
20+ */
1621 protected $ _config ;
1722
18- public function __construct (PushNotificationRepository $ PushNotificationRepository )
23+ /**
24+ * PushNotificationRepository object
25+ *
26+ * @var array
27+ */
28+ protected $ pushNotificationRepository ;
29+
30+ /**
31+ * Create a new controller instance.
32+ *
33+ * @param \Webkul\Core\Repositories\CoreConfigRepository $coreConfig
34+ * @return void
35+ */
36+ public function __construct (PushNotificationRepository $ pushNotificationRepository )
1937 {
20- $ this ->PushNotificationRepository = $ PushNotificationRepository ;
38+ $ this ->pushNotificationRepository = $ pushNotificationRepository ;
2139
2240 $ this ->_config = request ('_config ' );
2341 }
42+
2443 /**
2544 * Display a listing of the resource.
2645 *
@@ -49,7 +68,6 @@ public function create()
4968 */
5069 public function store (Request $ request )
5170 {
52-
5371 // $this->validate($request, [
5472 // 'title' => 'required',
5573 // 'description' => 'required',
@@ -61,25 +79,14 @@ public function store(Request $request)
6179 $ data = $ request ->all ();
6280
6381 // call the repository
64- $ pushnotification = $ this ->PushNotificationRepository ->create ($ data );
82+ $ this ->pushNotificationRepository ->create ($ data );
6583
6684 // flash message
6785 session ()->flash ('success ' , trans ('admin::app.response.create-success ' , ['name ' => 'Push Notification ' ]));
6886
6987 return redirect ()->route ($ this ->_config ['redirect ' ]);
7088 }
7189
72- /**
73- * Display the specified resource.
74- *
75- * @param int $id
76- * @return \Illuminate\Http\Response
77- */
78- public function show ($ id )
79- {
80-
81- }
82-
8390 /**
8491 * Show the form for editing the specified resource.
8592 *
@@ -88,8 +95,7 @@ public function show($id)
8895 */
8996 public function edit ($ id )
9097 {
91-
92- $ pushnotification = $ this ->PushNotificationRepository ->find ($ id );
98+ $ pushnotification = $ this ->pushNotificationRepository ->find ($ id );
9399
94100 return view ($ this ->_config ['view ' ], compact ('pushnotification ' ));
95101 }
@@ -103,8 +109,7 @@ public function edit($id)
103109 */
104110 public function update (Request $ request , $ id )
105111 {
106-
107- $ pushnotification = $ this ->PushNotificationRepository ->update (request ()->all (), $ id );
112+ $ this ->pushNotificationRepository ->update (request ()->all (), $ id );
108113
109114 session ()->flash ('success ' , trans ('admin::app.response.update-success ' , ['name ' => 'Push Notification ' ]));
110115
@@ -119,65 +124,68 @@ public function update(Request $request, $id)
119124 */
120125 public function destroy ($ id )
121126 {
122- $ pushnotification = $ this ->PushNotificationRepository ->findOrFail ($ id );
123- $ this ->PushNotificationRepository ->delete ($ id );
127+ $ pushnotification = $ this ->pushNotificationRepository ->findOrFail ($ id );
128+
129+ $ this ->pushNotificationRepository ->delete ($ id );
130+
124131 session ()->flash ('success ' , trans ('admin::app.response.delete-success ' , ['name ' => 'Push Notification ' ]));
132+
125133 return redirect ()->back ();
126134 }
127135
128136 public function pushtofirebase ($ id ) // send push notification to multiple devices
129137 {
130- $ pushnotification = \DB ::table ('push_notifications ' )->find ($ id );
138+ $ pushnotification = $ this ->pushNotificationRepository ->findOrFail ($ id );
139+
131140 $ title = $ pushnotification ->title ;
132141 $ body = $ pushnotification ->description ;
133142 $ badge = $ pushnotification ->imageurl ;
134143 $ targeturl = $ pushnotification ->targeturl ;
135144
136145 $ url = 'https://fcm.googleapis.com/fcm/send ' ;
137146
138- $ topic = 'bagisto ' ;
139-
140- $ fields = array (
141- 'to ' => '/topics/bagisto ' ,
142- 'data ' => [
143- 'body ' => $ body ,
144- 'title ' => $ title ,
145- 'click_action ' => $ targeturl
146- ]
147+ $ topic = 'bagisto ' ;
147148
148- );
149+ $ fields = array (
150+ 'to ' => '/topics/ ' . $ topic ,
151+ 'data ' => [
152+ 'body ' => $ body ,
153+ 'title ' => $ title ,
154+ 'click_action ' => $ targeturl
155+ ]
156+ );
149157
150- $ server_key = "AIzaSyBjbet3YzHEAp-YEkRN50zWx3asw0d07MA " ;
158+ $ server_key = "AIzaSyBjbet3YzHEAp-YEkRN50zWx3asw0d07MA " ;
151159
152- $ headers = array (
153- 'Content-Type:application/json ' ,
154- 'Authorization:key= ' . $ server_key ,
155- );
160+ $ headers = array (
161+ 'Content-Type:application/json ' ,
162+ 'Authorization:key= ' . $ server_key ,
163+ );
156164
157- // Open connection
158- $ ch = curl_init ();
165+ // Open connection
166+ $ ch = curl_init ();
159167
160- curl_setopt ( $ ch , CURLOPT_URL , $ url );
168+ curl_setopt ( $ ch , CURLOPT_URL , $ url );
161169
162- curl_setopt ( $ ch , CURLOPT_POST , true );
170+ curl_setopt ( $ ch , CURLOPT_POST , true );
163171
164- curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
172+ curl_setopt ( $ ch , CURLOPT_HTTPHEADER , $ headers );
165173
166- curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
167- // Disabling SSL Certificate support temporarly
168- curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , true );
174+ curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
175+ // Disabling SSL Certificate support temporarly
176+ curl_setopt ( $ ch , CURLOPT_SSL_VERIFYPEER , true );
169177
170- curl_setopt ( $ ch , CURLOPT_POSTFIELDS , json_encode ( $ fields ) );
171- // Execute post
172- $ result = curl_exec ( $ ch );
178+ curl_setopt ( $ ch , CURLOPT_POSTFIELDS , json_encode ( $ fields ) );
179+ // Execute post
180+ $ result = curl_exec ( $ ch );
173181
174- if ( $ result === false ) {
175- die ('Curl failed: ' . curl_error ($ ch ));
176- }
182+ if ( $ result === false ) {
183+ die ('Curl failed: ' . curl_error ($ ch ));
184+ }
177185
178- curl_close ( $ ch );
186+ curl_close ( $ ch );
179187
180- // Close connection
181- return redirect ()->back ();
188+ // Close connection
189+ return redirect ()->back ();
182190 }
183191}
0 commit comments