0

我想實現一個android應用程序的通知服務器。我遵循教程http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/和它的工作正常。但是現在,服務器端的所有內容都由我的通知服務器處理,我通過它與用戶登錄時共享我的設備ID和userId。我的通知服務器成功將regid發送到gcm,但我沒有收到我設備上的通知。我的onReceive()現在沒有被調用。這是我的服務器從gcm收到的響應。 MESSAGEID = 0:1463550332634647%fd0791fdf9fd7ecdNot received從通知服務器發送的GCM通知

Config.java

public interface Config { 

//static final String APP_SERVER_URL = "http://192.168.100.27:8081/GCMServer/GCMNotification?shareRegId=1";//earlier it was used to share regid with server 
static final String GOOGLE_PROJECT_ID = "299876636766"; 
static final String MESSAGE_KEY = "message"; 

} 

GCMNotificationIntentService.java

public class GCMNotificationIntentService extends IntentService { 

public static final int NOTIFICATION_ID = 1; 
private NotificationManager mNotificationManager; 
NotificationCompat.Builder builder; 

public GCMNotificationIntentService() { 
    super("GcmIntentService"); 
} 

public static final String TAG = "GCMNotificationIntentService"; 

@Override 
protected void onHandleIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this); 

    String messageType = gcm.getMessageType(intent); 
    System.out.println("MessageType is"+messageType); 
    String msg = intent.getStringExtra("message"); 
    System.out.println("mESSAGE IS---->>>"+msg); 

    if (!extras.isEmpty()) { 
     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()); 
     } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE 
       .equals(messageType)) { 

      for (int i = 0; i < 3; i++) { 
       Log.i(TAG, 
         "Working... " + (i + 1) + "/5 @ " 
           + SystemClock.elapsedRealtime()); 
       try { 
        Thread.sleep(5000); 
       } catch (InterruptedException e) { 
       } 

      } 
      Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime()); 

      sendNotification(""+msg); 
      Log.i(TAG, "Received: " + extras.toString()); 
     } 
    } 
    GcmBroadcastReceiver.completeWakefulIntent(intent); 
} 

private void sendNotification(String msg) { 
    Log.d(TAG, "Preparing to send notification...: " + msg); 
    System.out.println("Message is"+msg); 
    mNotificationManager = (NotificationManager) this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    PendingIntent contentIntent=null ; 
    if(msg.equalsIgnoreCase("call")){ 
     System.out.println("Inside iffffffffffffffffffffff"); 
     PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 

     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag"); 
     wl.acquire(); 
     Intent i = new Intent(getApplicationContext(), MainActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(i); 
     //setResultCode(Activity.RESULT_OK) 

     wl.release(); 
     contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, SplashScreen.class), 0); 
     System.out.println("the content intent is"+contentIntent.toString()); 

    }else{ 
     System.out.println("Inside else"); 
     contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 


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

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

    Log.d(TAG, "Notification sent successfully."); 
} 
} 

GCMBroadcastReceiver.java

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { 

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

你有沒有得到任何錯誤日誌?並讓你在php文件中更改密鑰? –

+0

GCM服務器的迴應是什麼? –

+0

@NiranjPatel messageId(長字符串)和規範註冊編號 – SwagDevelopers

回答

1

如果從獲得成功的消息像下面GCM服務器,那麼你的設備必須收到通知

成功消息從GCM服務器

{ 
    "multicast_id": 661164****4469281, 
    "success": 1, 
    "failure": 0, 
    "canonical_ids": 1, 
    "results": [{ 
     "registration_id": "APA91bFWKKC4Bh_B******-H", 
     "message_id": "0:14635506***b6f9fd7ecd" 
    }] 
} 

可低於對於在設備未接收到的消息的情況。

  1. 在android側的錯誤發件人ID。
  2. 服務器端(PHP)錯誤的Google API密鑰。
  3. 錯誤的設備註冊ID您要發送到GCM服務器。

請立即驗證所有值並檢查。 如果一切都很完美,那麼你必須在設備中收到消息。 請檢查您的設備中的互聯網連接。

請驗證您的Menifest.xml文件以下。

<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" /> 
       <!-- Receives the registration id. --> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

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

     <service android:name=".GCMIntentService" /> 
    </application> 

    <!-- GCM connects to Internet Services. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 

    <!-- 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. --> 
    <permission 
     android:name="your_package.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="your_package.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" /> 
+0

我發送電子郵件regId從regId = gcm.register()接收郵件。 Api鍵也是正確的。請檢查兩次。 – SwagDevelopers

+0

我看到這篇文章http://stackoverflow.com/questions/21515141/broadcastreceiver-is-never-called-using-gcm-service好像有類似的問題,但解決方案和理由我不明白。 – SwagDevelopers