2017-02-15 116 views
8

我收到一個奇怪的問題,使用FCM向android發送推送通知。FCM推送通知問題: - 「錯誤」:「未註冊」

目標: - 有錯誤在發送推送通知

下面是我做的有功能發送推送通知到Android

public static function SendMultipleNotificationAndroid($groups) 
    { 
     //your api key SERVER API KEY 
     $apiKey = Yii::$app->params['android_api_key']; 
     $url = 'https://fcm.googleapis.com/fcm/send';  
     $headers = array(
      'Authorization:key=' . $apiKey, 
      'Content-Type: application/json' 
     ); 

     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

     foreach($groups as $resG){ 
      $users = $resG['users'];       
      $msg = $resG['message']; 
      $type = $resG['notification_type']; 
      $notification_data = $resG['notification_data']; 

      $deviceTokens = []; 
      foreach($users as $resUser){ 
       $deviceTokens[] = $resUser['device_token']; 
       //Add Friend badge count +1 
       Common::AddRemoveBadgeCount($resUser['user_id']); 
      } 
      if(!empty($deviceTokens)){ 
       $fields = array(
        'registration_ids' => $deviceTokens, 
        'priority'  => 'high', 
        'collapse_key' => $resG['notification_type'], 
        'time_to_live' => 2419200,  
        "click_action" =>"NotificationListingActivity",  
        'data'   => [     
         "title"    => "ProjectName", 
         "body"    => $resG['message'], 
         "action_tag"  => $resG['notification_type'], 
         "message"   => $resG['message'], 
         'notification_type' => $type, 
         'notification_data' => $notification_data, 
         'sound'    => 'default', 
        ] 
       ); 
       //Print result 
       p($ch,0); 
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
       curl_exec($ch); 
      }    
     } 
     curl_close($ch); 
    } 

場景那麼問題是,當我發送單通知它工作正常,但是當我發送多個通知我得到錯誤每次

<pre>Resource id #5</pre>{"multicast_id":4818908994630396118,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136045570022%c3bae3c6002e9358"}]} 

<pre>Resource id #5</pre>{"multicast_id":5218359780835228544,"success":1,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"},{"message_id":"0:1487136046618669%c3bae3c6002e9358"}]} 

當我們調試代碼時,我們的數據庫中沒有設備令牌,沒有防火牆停止發送推送通知。

我每次調用上面的函數,我得到

「錯誤」: 「NotRegistered」

任何幫助,將不勝感激。

在此先感謝。

回答

5

不知道很多關於PHP,但最近我在另一個項目中所面臨的同樣的問題,我已經解決了這種方式:

參考,這首: Where can I find the API KEY for Firebase Cloud Messaging?

,並得到更新的API密鑰如下圖所示enter image description here

+0

可能是這種情況讓我看看這個問題,並嘗試!我會盡快回復您!謝謝! –

+0

Bravo!加工! –

+0

@maddy這沒有爲我工作。我在提出請求時更改了服務器密鑰,但仍遇到同樣的錯誤。 – AIon