From 25a48b79623e109d15873472eb95dfb47616e72b Mon Sep 17 00:00:00 2001
From: W <W>
Date: Mon, 25 Feb 2019 23:37:17 +0700
Subject: [PATCH] fixing fcm

---
 app/Http/Controllers/CommentController.php | 76 ++++++++++++----------
 1 file changed, 43 insertions(+), 33 deletions(-)

diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php
index 8ca3e91..4109396 100644
--- a/app/Http/Controllers/CommentController.php
+++ b/app/Http/Controllers/CommentController.php
@@ -34,40 +34,50 @@ class CommentController extends Controller
         $request->validate([
             'comment' => 'required',
         ]);
+        $task = Tasks::where([
+            'id' => $taskId
+        ])->first();
+        if (is_null($task)) {
+            return response(json_encode([
+                'data' => NULL,
+                'statusMessage' => 'error'
+            ]), 404);
+        } else {
 
-        $comment = Comment::create([
-            'comment' => $request->comment,
-            'task_id' => $taskId,
-            'user_id' => $userId          
-        ]);
-        //FireBaseeeee
-        $serviceAccount = ServiceAccount::fromArray(Config::get('constants.GOOGLE_FIREBASE_SERVICE'));
-        $firebase = (new Factory)
-            ->withServiceAccount($serviceAccount)
-            ->create();
-    
-        $messaging = $firebase->getMessaging();
-    
-        $notification = Notification::fromArray([
-            'title' => 'Dapet comment nih',
-            'body' => $request->comment
-        ]);
-    
-        $data = [
-            'task_id' => $taskId,
-            'task_id' => $taskId
-        ];
-
-        $message = CloudMessage::withTarget('topic', 'whe')
-        ->withNotification($notification)
-        ->withData($data); // optional;
-    
-        $messaging->send($message);
+            $comment = Comment::create([
+                'comment' => $request->comment,
+                'task_id' => $taskId,
+                'user_id' => $userId          
+            ]);
+            //FireBaseeeee
+            $serviceAccount = ServiceAccount::fromArray(Config::get('constants.GOOGLE_FIREBASE_SERVICE'));
+            $firebase = (new Factory)
+                ->withServiceAccount($serviceAccount)
+                ->create();
+        
+            $messaging = $firebase->getMessaging();
+        
+            $notification = Notification::fromArray([
+                'title' => 'Dapet comment nih',
+                'body' => $request->comment
+            ]);
+        
+            $data = [
+                'task_id' => $taskId,
+                'user_id' => $userId
+            ];
+            $groupId = (string) $task->group_id;
+            $message = CloudMessage::withTarget('topic', $groupId)
+            ->withNotification($notification)
+            ->withData($data); // optional;
+        
+            $messaging->send($message);
 
-        //////////////
-        return response(json_encode([
-            'data' => NULL,
-            'statusMessage' => 'success'
-        ]), 200);
+            //////////////
+            return response(json_encode([
+                'data' => NULL,
+                'statusMessage' => 'success'
+            ]), 200);
+        }
     }
 }
-- 
GitLab