| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MedianetDev\CloudMessage\Drivers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use MedianetDev\CloudMessage\Contracts\NotificationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class HuaweiNotification implements NotificationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     use Notification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private static $urlAuth = 'https://login.vmall.com/oauth2/token'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private static $pushUrl = 'https://push-api.cloud.huawei.com/v1/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public static function sendToAll(array $message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         // TODO: Send to all devices | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public static function sendToTokens(array $message, array $tokens) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $structureData = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $structureData['validate_only'] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $structureData['message']['data'] = json_encode($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $structureData['message']['token'] = $tokens; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $headers = ['Content-Type: application/json', 'Authorization: Bearer '.self::getAccessToken()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $url = self::$pushUrl.config('cloud_message.huawei.app_id').'/messages:send'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $response = self::request($url, json_encode($structureData), $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     // Other required interface methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     private static function getAccessToken() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $data = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'grant_type' => config('cloud_message.huawei.grant_type'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'client_id' => config('cloud_message.huawei.app_id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'client_secret' => config('cloud_message.huawei.app_secret'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $response = self::request(self::$urlAuth, http_build_query($data)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $result = json_decode($response['data'], true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return $result['access_token'] ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     public static function sendToTopic(array $message, string $topic) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public static function subscribeToTopic(string $topic, array $tokens) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public static function unsubscribeToTopic(string $topic, array $tokens) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |  |