2016-01-20 139 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

相关问题