2016-01-20 130 views
0

我的應用程序將不會收到來自GCM任何消息,當生成數量和接觸的活動發送它,我接受它在mysql中這樣##### ##:################### aWvJRwO98DIipu8lhqrH7CWYSkHDqv_Cn4liO49HMizICLwZ該應用程序將不會收到消息,並獲得「NotRegistered」響應與GCM

所以我用整體registration_ids數,但是我在響應得到錯誤「NotRegistered」,我不知道哪裏是我還測試了通過測試的網站

其響應發送消息此頁問題

<?php 

class GSM { 

function _construct() { 

} 

    public function send_notification($registration_ids, $message){ 

     include_once './config.php'; 


     $url = 'https://gcm-http.googleapis.com/gcm/send'; 

     $fileds = array(
      'registration_ids' => $registration_ids, 
      'message' => $message,); 


     $headers = array ( 
       'Authorization: key=' . GOOGLE_API_KEY, 
       'Content-Type: application/json' 
      ); 
     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fileds)); 

     $result = curl_exec($ch); 
     if ($result === false) { 

      die('Cutl failed:' . curl_error($ch)); 
     } 
     curl_close($ch); 
     echo $result; 
    } 
} 



?> 

getRegId在Contact類

public void getRegId() { 
new AsyncTask<Void, Void, String>() { 

    @Override 
    protected String doInBackground(Void... params) { 

     String msg = ""; 

     try { 
      if (gcm == null) { 
       InstanceID instanceID = InstanceID.getInstance(getActivity().getApplicationContext()); 
       regId = instanceID.getToken(projectNumber, GoogleCloudMessaging.INSTANCE_ID_SCOPE); 

       msg = "Device registration, registration ID=" + regId; 

       Log.i(TAG, msg); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
      msg = "Error :" + e.getMessage(); 
     } 
     return msg; 
    } 

}.execute(null, null, null); 

} 

} 

類接收消息

public class GCMIntentService extends GcmListenerService{ 


    private static final String TAG = "GCMIntentService"; 

@Override 
public void onMessageReceived(String from, Bundle data) { 

    String message = data.getString("message"); 
    Log.d(TAG, "from:" + from); 
    Log.d(TAG, "message:" + message); 

    sendNotification(message); 
} 
private void sendNotification(String message){ 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){ 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.logo) 
       .setContentTitle("New Message") 
       .setContentText(message) 
       .setAutoCancel(true) 
       .setSound(defaultSound) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0, notificationBuilder.build()); 

    } 

} 
} 

AndroidManifest.xml中

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
<uses-permission android:name="android.permission.VIBRATE"/> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<permission 
    android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission android:name="com.example.abdul_majeed.alruthea.permission.C2D_MESSAGE" /> 

......... 

<service 
      android:name=".GCMIntentService" 
      android:exported="false" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      </intent-filter> 
     </service> 

我還添加了

dependencies { 
    classpath 'com.android.tools.build:gradle:2.0.0-alpha3' 
    classpath 'com.google.gms:google-services:2.0.0-alpha3' 

apply plugin: 'com.android.application' 

    ......... 

dependencies { 
    compile "com.google.android.gms:play-services:8.4.0" 
} 

......... 

apply plugin: 'com.google.gms.google-services' 
+0

如果您的設備生成的註冊ID那麼你可以通過在Android REST客戶端發送它像'postman'或'提前休息client' – Pankaj

+0

檢查[此鏈接]查看通知(http://stackoverflow.com/a/34870948/2715073)從靜止客戶端使用發送短信推進REST客戶端 – Pankaj

+0

我沒有,但不會收到任何消息,我的設備是虛擬的genymotion – ahmed

回答

0

檢查這是你產生是在谷歌API的開發者控制檯正確,同樣在你通過瀏覽器密鑰和UDID您的PHP文件您的瀏覽器密鑰。

您可以通過 http://www.androidbegin.com/tutorial/gcm.html

+0

此頁不起作用! – ahmed

+0

在此 谷歌API密鑰(與IP鎖定)是你的:你在谷歌開發者控制檯做 和 器械註冊ID的瀏覽器鍵:在生成的Android設備 上的消息是你推送消息。 – 2016-01-20 11:17:19

+0

哦對不起我嘗試它,我得到{ 「multicast_id」:62114168555656533, 「成功」:0, 「失敗」:1, 「canonical_ids」:0, 「結果」:[{ 「錯誤」: 「NotRegistered」}]} – ahmed

相關問題