2016-03-08 80 views
0

我正在實施GCM。我已經註冊了我的谷歌開發者控制檯應用程序,使GCM API服務,谷歌的地方,services.json在app文件夾,放在清單如下:Android GCM - 未獲取註冊令牌

<uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="<my-package-name>.permission.C2D_MESSAGE" /> 

<application>.. 
<meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <!-- GCM --> 
     <receiver 
      android:name="com.google.android.gms.gcm.GcmReceiver" 
      android:exported="true" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <action android:name="android.net.conn." /> 

       <category android:name="<my-pakage-name>" /> 
      </intent-filter> 
     </receiver> 

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

     <service 
      android:name=".notifications.InstanceIDListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.gms.iid.InstanceID" /> 
      </intent-filter> 
     </service> 

     <service 
      android:name=".notifications.RegistrationIntentService" 
      android:exported="false"></service> 
</application> 

應用級的build.gradle

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

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.google.android.gms:play-services:8.1.0' 
    compile 'com.google.android.gms:play-services-gcm:8.1.0' 
} 

項目級build.gradle

dependencies { 
     classpath 'com.android.tools.build:gradle:1.5.0' 
     classpath 'com.google.gms:google-services:1.4.0-beta3' 
    } 

因此,我的查詢是,我不知道什麼是錯誤的上面,我amot獲取註冊令牌。請幫助我獲得註冊令牌

+0

你在日誌中看到什麼? –

+0

與GCM無關.. – Mohanish

+0

您是否在註冊時添加了正確的發件人ID? –

回答

0

您需要創建自己的Receiver和Service並像這樣使用它。

接收機:

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v4.content.WakefulBroadcastReceiver; 


public class GCMMessageReciever extends WakefulBroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     ComponentName comp = new ComponentName(context.getPackageName(), 
       GCMIntentService.class.getName()); 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 

    } 
} 

服務:

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.SharedPreferences.Editor; 
import android.preference.PreferenceManager; 
import android.support.v4.app.NotificationCompat; 
import android.support.v4.content.LocalBroadcastManager; 

import com.appdupe.uberforxserviceprovider.R; 
import com.google.android.gcm.GCMBaseIntentService; 
import com.uber.driver.MyMainFragmentActivity; 
import com.uber.driver.constants.Constants; 
import com.uber.driver.gcm.ServerUtilities; 
import com.uber.driver.util.Utils; 

public class GCMIntentService extends GCMBaseIntentService { 

    private NotificationManager mNotificationManager; 

    public GCMIntentService() { 
     super(Constants.SENDER_ID); 
    } 

    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     Intent i = new Intent(Constants.PUSHNOTIFICATION); 
     i.putExtra(Constants.DB_PHONE_GCM_ID, registrationId); 
     LocalBroadcastManager.getInstance(context).sendBroadcast(i); 
     ServerUtilities.register(context, "name", "email", registrationId); 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 
     String jsonString = intent.getExtras().getString("message"); 

     try { 
      SharedPreferences preferences = PreferenceManager 
        .getDefaultSharedPreferences(getApplicationContext()); 
      Editor editor = preferences.edit(); 

      JSONObject jsonObject = new JSONObject(jsonString); 
      if (jsonObject.getString("id").equals("1")) { 
       editor.putFloat(Constants.USER_LATTITUDE, 
         Float.parseFloat(jsonObject.getString("lattitude"))); 
       editor.putFloat(Constants.USER_LOGITUDE, 
         Float.parseFloat(jsonObject.getString("logitude"))); 
       editor.putString(Constants.USER_RANDOM_ID, 
         jsonObject.getString("random_id")); 
       editor.putBoolean(Constants.IS_USER_SET, true); 
       editor.putString(Constants.PHONE_CLIENT, 
         jsonObject.getString("client_contact")); 
       editor.putString(Constants.PHONE_OPEARTOR, 
         jsonObject.getString("operator_contact")); 
       // editor.putBoolean(Constants.IS_USER_SET, true); 
       editor.putString(Constants.CLIENT_NAME, 
         jsonObject.getString("client_name")); 
       sendNotification(jsonString); 
      } else { 
       editor.putBoolean(Constants.DB_IS_JOB_DONE, true); 
       editor.putInt(Constants.FRAGMENT_POSITION, 
         Constants.FRAGMENT_MAP); 
       editor.putString(Constants.USER_RANDOM_ID, ""); 
      } 
      editor.commit(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     Utils.log("Received message.. " + jsonString); 
     Intent i = new Intent(Constants.PUSHNOTIFICATION); 
     i.putExtra("json", jsonString); 
     LocalBroadcastManager.getInstance(context).sendBroadcast(i); 
    } 

    @Override 
    protected void onDeletedMessages(Context context, int total) { 
    } 

    @Override 
    public void onError(Context context, String errorId) { 
    } 

    @Override 
    protected boolean onRecoverableError(Context context, String errorId) { 
     return super.onRecoverableError(context, errorId); 
    } 

    @Override 
    protected void onUnregistered(Context arg0, String arg1) { 

    } 

    private void sendNotification(String msg) { 

     mNotificationManager = (NotificationManager) this 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MyMainFragmentActivity.class), 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle(
         getResources().getString(R.string.text_uber_driver)) 
       .setStyle(
         new NotificationCompat.BigTextStyle() 
           .bigText(getResources().getString(
             R.string.text_job_assigned))) 
       .setDefaults(
         Notification.DEFAULT_SOUND 
           | Notification.DEFAULT_VIBRATE); 

     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setAutoCancel(true); 

     Notification notification = mBuilder.build(); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     mNotificationManager 
       .notify(Constants.NOTIFICATION_ID, mBuilder.build()); 
    } 
} 

Android清單:

<application>.. 
<meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
     <!-- GCM --> 
     <receiver 
      android:name=".GCMMessageReciever" 
      android:exported="true" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
       <action android:name="android.net.conn." /> 

       <category android:name="<my-pakage-name>" /> 
      </intent-filter> 
     </receiver> 

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

     <service 
      android:name=".notifications.InstanceIDListenerService" 
      android:exported="false"> 
      <intent-filter> 
       <action android:name="com.google.android.gms.iid.InstanceID" /> 
      </intent-filter> 
     </service> 

     <service 
      android:name=".notifications.RegistrationIntentService" 
      android:exported="false"></service> 
</application>