通过PHP发送后,GCM通知没有任何反应

时间:2014-06-22 17:21:58

标签: php android google-cloud-messaging

任何人都可以看到做错了吗?我试图使用GCM向Android设备发送推送通知

//Google cloud messaging GCM-API url
    $url = 'https://android.googleapis.com/gcm/send';
    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );
// Google Cloud Messaging GCM API Key
define("GOOGLE_API_KEY", "***********************");    
    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    $result = curl_exec($ch);       
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }
    curl_close($ch);
    echo 'GCM sent succesfull '.$result;

没有任何事情它只是返回GCM发送成功并跟随结果可能是什么问题?

我使用的服务器应用程序密钥和浏览器应用程序仍然没有结果,或者它是注册ID还是我需要设备ID(虽然我知道GCM不再使用设备ID)? 但这就是我的注册ID的样子

  

APA91bFGOnPlE57RBK-dzZDqar__j8pdT7ZOhjHgfJfRdCwGQyWi_vcsxgvs4jtibRg3TbNmPgD_PrQLEIysEtSpzV3iRFEBbbeCTlR_0P9Wvl6JeQwtmud97DSmBFFPyrjNdX3a6AV9hZrylO8gyzcqGkAGZoZH0A

我得到了这个结果

  

{" multicast_id":7629022022432875138,"成功":1,"失效":0," canonical_ids":0,&# 34;结果":[{" MESSAGE_ID":" 0:1403458472513342%2a7e323af9fd7ecd"}]}

我的GCM广播接收器

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i("GCM REG", "In Brocast Reciever.... Receiving Something...     ");
        ComponentName comp = new ComponentName(context.getPackageName(),
            GCMNotificationIntentService.class.getName());
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
        Log.i("GCM REG", "In Brocast Reciever.... Registered");
      }

}

我的GCM BroadCast Intent Notification Service Reciever

public class GCMNotificationIntentService extends IntentService {

private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

public GCMNotificationIntentService() {
super("GcmIntentService");
Log.i("GCM REG", "GCM Notification Started");
}



@Override
protected void onHandleIntent(Intent intent) {
 Log.i("GCM REG", "GCM Notification Handling it");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

String messageType = gcm.getMessageType(intent);

if (!extras.isEmpty()) {
  if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
      .equals(messageType)) {
    sendNotification("Send error: " + extras.toString());
    Log.i("GCM REG", "Send Error");
  } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
      .equals(messageType)) {
    sendNotification("Deleted messages on server: "
        + extras.toString());
    Log.i("GCM REG", "Deleted messages on server");
  } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
      .equals(messageType)) {

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

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

    sendNotification("Message Received from Google GCM Server: "
        + extras.get(Config.MESSAGE_KEY));
    Log.i(Config.TAG, "Received: " + extras.toString());
  }else
  {
      Log.i("GCM REG", "Bundle is not empty but else...Registeration i guess");
      sendNotification("Welcome, To boosalert");
  }
}else
{
  Log.i("GCM REG", "Bundle is empty");
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
sendNotification("Bundle is empty");
 }

  private void sendNotification(String msg) {
Log.i(Config.TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
    .getSystemService(Context.NOTIFICATION_SERVICE);

 PendingIntent 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(Config.NOTIFICATION_ID, mBuilder.build());
 Log.i(Config.TAG, "Notification sent successfully.");
 }
 }

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.gbaalert"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="example.gbaalert.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<!-- uses-permission android:name="example.gbaalert.permission.C2D_MESSAGE" / -->


<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="example.gbaalert.login"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.signin"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <action android:name="android.intent.action.DELETE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="com.idrivecare.familypro" />
        </intent-filter>
    </activity>
    <activity
        android:name="example.gbaalert.main"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name="example.gbaalert.broadcastfragment"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.friendsfragment"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.homefragment"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.search"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustNothing" >
    </activity>
    <activity
        android:name="example.gbaalert.chat"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.settingsfrag"
        android:configChanges="orientation|keyboardHidden"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name="example.gbaalert.profileinfofrag"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name="example.gbaalert.commentActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name="example.gbaalert.viewsearchtimeline"
        android:label="@string/app_name"
        android:windowSoftInputMode="stateHidden" >
    </activity>
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

    <receiver
        android:name="example.gbaalert.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="example.gbaalert" />
        </intent-filter>
    </receiver>

    <service android:name="example.gbaalert.GCMNotificationIntentService" />

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name" >
    </activity>
</application>

注:

  

我使用蓝色堆栈来测试我的应用程序,这可能是问题,因为我似乎无法在使用bluestacks时进行调试,而且我使用的GenyMotion没有启用google play服务来测试和查看错误

1 个答案:

答案 0 :(得分:0)

我能看到的唯一问题是清单中的这条注释行:

<!-- uses-permission android:name="example.gbaalert.permission.C2D_MESSAGE" / -->

你应该取消评论。