2014-08-27 3 views
2

내 휴대 전화에서 GCM을 테스트하고 있습니다. (2.3.6 안드로이드).Android GCM 서버가 전송되었지만 GCM이 기기에 푸시하지 않음

매니페스트 파일 (MainActivity, First 및 Second 활동은 아무 것도하지 않으며, 다른 테스트 목적을 위해 GCM을 방해하지 않습니다.)

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.testconnectionapp" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<!-- GCM requires a Google account. --> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<!-- Keeps the processor from sleeping when a message is received. --> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<!-- 
Creates a custom permission so only this app can receive its messages. 

NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE, 
     where PACKAGE is the application's package name. 
--> 
<permission 
    android:name="com.example.testconnectionapp.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

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

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

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="21" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

    <activity 
     android:name=".DemoActivity" 
     android:label="@string/app_name" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
      android:launchMode="singleTop"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name=".GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <!-- Receives the actual messages. --> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <category android:name="com.google.android.gcm.demo.app" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".GcmIntentService" /> 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <!-- <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> --> 
    </activity> 
    <activity 
     android:name=".First" 
     android:label="@string/title_activity_first" > 
    </activity> 
    <activity 
     android:name=".Second" 
     android:label="@string/title_activity_second" > 
    </activity> 
    <activity 
     android:name=".SQLiteCrudExample" 
     android:label="@string/title_activity_second" > 
    </activity> 
</application> 

</manifest> 

GCMBroadcastReceiver

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // Explicitly specify that GcmIntentService will handle the intent. 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      GcmIntentService.class.getName()); 
    // Start the service, keeping the device awake while it is launching. 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 
} 

GCMIntentSErvice가 URL에 'POST'요청을 보내기

을 같이 이제

public class GcmIntentService extends IntentService { 
public static final int NOTIFICATION_ID = 1; 
private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 

public GcmIntentService() { 
    super("GcmIntentService"); 
} 
public static final String TAG = "GCM Demo"; 

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 
    // The getMessageType() intent parameter must be the intent you received 
    // in your BroadcastReceiver. 
    String messageType = gcm.getMessageType(intent); 

    if (!extras.isEmpty()) { // has effect of unparcelling Bundle 
     /* 
     * Filter messages based on message type. Since it is likely that GCM will be 
     * extended in the future with new message types, just ignore any message types you're 
     * not interested in, or that you don't recognize. 
     */ 
     if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) { 
      sendNotification("Send error: " + extras.toString()); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) { 
      sendNotification("Deleted messages on server: " + extras.toString()); 
     // If it's a regular GCM message, do some work. 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) { 
      // This loop represents the service doing some work. 
      for (int i = 0; i < 5; i++) { 
       Log.i(TAG, "Working... " + (i + 1) 
         + "/5 @ " + SystemClock.elapsedRealtime()); 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
       } 
      } 
      Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 
      // Post notification of received message. 
      sendNotification("Received: " + extras.toString()); 
      Log.i(TAG, "Received: " + extras.toString()); 
     } 
    } 
    // Release the wake lock provided by the WakefulBroadcastReceiver. 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

// Put the message into a notification and post it. 
// This is just one simple example of what you might choose to do with 
// a GCM message. 
private void sendNotification(String msg) { 
    mNotificationManager = (NotificationManager) 
      this.getSystemService(Context.NOTIFICATION_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      new Intent(this, DemoActivity.class), 0); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.abc_ab_solid_dark_holo) 
    .setContentTitle("GCM Notification") 
    .setStyle(new NotificationCompat.BigTextStyle() 
    .bigText(msg)) 
    .setContentText(msg); 

    mBuilder.setContentIntent(contentIntent); 
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); 
} 
} 

, 나는 서버에서 메시지를 보낼 수 있습니다 https://android.googleapis.com/gcm/send 응답 코드 : 200 { "multicast_id": 5410443299609525979, "성공": 1, "실패": 0, "canonical_ids": 0, "결과": [{ "message_id": "0 : 1409160055237618 % a13ced80f9fd7ecd"}]}

그러나 그 후에는 휴대 전화에서 알림이 생성되는 것을 볼 수 없습니다. 여기서 무슨 일이 일어나고 있는지. 이것에 대한 가능한 이유는 무엇일까요?

저는 집에서도 Wi-Fi에 연결되어 있고, 데이터 팩을 내 휴대폰에서 활성화했기 때문에 wifi를 배포하더라도 어쨌든 영향을 미칠 이유는 확실하지 않지만 해결책은 here suggested입니다. 생성 된 모든 통지.

도움이 될 것입니다.

답변

1

수신자가 잘못되었습니다 (<category android:name="com.google.android.gcm.demo.app" />). 그것이 앱의 패키지 이름이라고 가정하면 com.example.testconnectionapp으로 변경하십시오.

+0

어리석은 날. 이런 질문에 괴롭혀 야합니다 :) 감사합니다. – Kraken

+0

@ 크라켄 때로는 중복 질문을 찾는 것보다 대답하는 것이 더 빠릅니다 :) – Eran

+0

안녕하세요, 제 생각에 가장 최근의 질문을 살펴보실 수 있습니까? 그래도 고마워. – Kraken

관련 문제