2015-10-22 48 views
0

我沒有使用谷歌的我是創建一個服務器密鑰獲得來自谷歌的設備的註冊ID。甚至不叫GCMIntentService
..我沒有從谷歌獲得設備的註冊ID。甚至不叫GCMIntentService

下面

是我的清單文件 ,但沒有得到來自谷歌

package="com.example.isquare.pushexample" > 

<receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" 
      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" /> 
       <category android:name="com.example.isquare.pushexample" /> 
      </intent-filter> 
     </receiver> 

     <service android:name=".MyIntentService" /> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <!-- GCM requires a Google account. --> 
    <permission 
     android:name="com.example.isquare.pushexample.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.isquare.pushexample.permission.C2D_MESSAGE" /> 

    <!-- This app has permission to register and receive data message. --> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <!-- Network State Permissions to detect Internet status --> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <!-- Permission to vibrate --> 
    <uses-permission android:name="android.permission.VIBRATE" /> 

這裏的設備寄存器關鍵是我GCMIntentService

public class GCMIntentService extends GCMBaseIntentService { 


    private static final String TAG = "GCMIntentService"; 

    private Controller aController = null; 

    public GCMIntentService() { 
     // Call extended class Constructor GCMBaseIntentService 

     super(Config.GOOGLE_SENDER_ID); 
     System.out.println("the GCM ID IS "); 
    } 

    @Override 
    protected void onMessage(Context context, Intent intent) { 

     if(aController == null) 
      aController = (Controller) getApplicationContext(); 

     Log.i(TAG, "Received message"); 
     String message = intent.getExtras().getString("price"); 

     aController.displayMessageOnScreen(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

    @Override 
    protected void onError(Context context, String errorId) { 
     if(aController == null) 
      aController = (Controller) getApplicationContext(); 

     Log.i(TAG, "Received error: " + errorId); 
     aController.displayMessageOnScreen(context, 
       getString(R.string.gcm_error, errorId)); 
    } 

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

     if(aController == null) 
      aController = (Controller) getApplicationContext(); 

     Log.i(TAG, "Received deleted messages notification"); 
     String message = getString(R.string.gcm_deleted, total); 
     aController.displayMessageOnScreen(context, message); 
     // notifies user 
     generateNotification(context, message); 
    } 

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

    @Override 
    protected void onRegistered(Context context, String registrationId) { 
     //Get Global Controller Class object (see application tag in AndroidManifest.xml) 
     if(aController == null) 
      aController = (Controller) getApplicationContext(); 

     Log.i(TAG, "Device registered: regId = " + registrationId); 
     aController.displayMessageOnScreen(context, 
       "Your device registred with GCM"); 
     Log.d("NAME", MainActivity.name); 
     aController.register(context, MainActivity.name, 
       MainActivity.email, registrationId); 
    } 

    @Override 
    protected void onUnregistered(Context context, String registrationId) { 
     if(aController == null) 
      aController = (Controller) getApplicationContext(); 
     Log.i(TAG, "Device unregistered"); 
     aController.displayMessageOnScreen(context, 
       getString(R.string.gcm_unregistered)); 
     aController.unregister(context, registrationId); 
    } 

    private static void generateNotification(Context context, String message) { 

     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 

     NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     //notification.sound = Uri.parse(
     "android.resource://" 
       + context.getPackageName() 
       + "your_sound_file_name.mp3"); 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 

    } 

廣播接收器:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE); 
     aController.acquireWakeLock(getApplicationContext()); 
     System.out.println(newMessage + ""); 
     Toast.makeText(getApplicationContext(), "Got Message: " + newMessage, Toast.LENGTH_LONG).show(); 
     aController.releaseWakeLock(); 
    } 
}; 

回答

1
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE); 
    aController.acquireWakeLock(getApplicationContext()); 
    System.out.println(newMessage + ""); 
    Toast.makeText(getApplicationContext(), "Got Message: " + newMessage, Toast.LENGTH_LONG).show(); 
    aController.releaseWakeLock(); 
} 

};

+0

Thanksss ... !!!!!有用 –

0

我沒有看到你所得到的寄存器ID。

if (mGoogleCloudMessaging == null) { 
    mGoogleCloudMessaging = GoogleCloudMessaging.getInstance(mContext); 
} 
regId = mGoogleCloudMessaging.register(C.google.SENDER_ID); 

這裏有一個工作示例: https://github.com/kweaver00/Android-Samples/tree/master/Push

後從註釋的詳細信息:

嘗試增加免去您的<receiver>有以下幾點:

<receiver android:name=".GCMBroadcastReceiver" 
     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"/> 
       <category android:name="YOUR_PACKAGE"/> 
     </intent-filter> 
</receiver> 

與創建文件GCMBroadcastReceiver.java以下代碼:

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver { 
    public static final String TAG = "GCM_GCMBroadcastReceiver_"; 
    @Override 
    public void onReceive(Context context, Intent intent){ 
     Log.v(TAG, "OnReceive was called."); 
     ComponentName comp = new ComponentName(context.getPackageName(), 
      MyIntentService.class.getName()); 
     startWakefulService(context, (intent.setComponent(comp))); 
     setResultCode(Activity.RESULT_OK); 
    } 
} 
+0

GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); final String regId = GCMRegistrar.getRegistrationId(this); System.out.println(「reg是」+ regId); (regId.equals(「」)){ GCMRegistrar.register(this,Config.GOOGLE_SENDER_ID); //注意:從配置中獲取發件人ID。 (「Registration」,「已經註冊,regId:」+ regId);其他{ }其他{ Log.v } –

+0

我假設這印有一個註冊ID? – Keith

+0

在我的Logcat我收到這條消息onReceive:com.google.android.c2dm.intent.REGISTRATION 10-22 19:35:35.236 1343-1343/com.example.pushexample V/GCMBroadcastReceiver:GCM IntentService class:com。 example.isquare.pushexample.GCMIntentService 10-22 19:35:35.236 1343-1343/com.example.pushexample V/GCMBaseIntentService:獲取喚醒鎖 –

相關問題