【问题标题】:GCM Notification nothing happens after sending via PHPGCM 通知通过 PHP 发送后没有任何反应
【发布时间】:2014-06-22 17:21:58
【问题描述】:

谁能看出做错了什么?我正在尝试使用 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_0P9Wvl6JeQwtmud97DSmBFFPyrj

结果我得到了这个

{"multicast_id":7629022022432875138,"success":1,"failure":0,"canonical_ids":0,"results":[{"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 广播意图通知服务接收器

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 服务供我测试和查看错误

【问题讨论】:

  • 您的服务器收到了成功响应,这意味着问题出在您的客户端代码中(或您的设备与互联网的连接)。请发布客户端代码(清单、广播接收器、服务等...)
  • @Eran 我认为它有效,因为我收到通知说我已注册到 GCM 服务器并且我得到了我的注册 ID
  • 注册部分有效,但接收 GCM 消息无效。你也可以发布你的清单吗?发送消息时,您是否在 Logcat 中看到任何日志打印? (例如——你看到In Brocast Reciever.... Receiving Something...
  • @Eran 请看一下编辑看看有没有问题?
  • 请看我的回答者

标签: php android google-cloud-messaging


【解决方案1】:

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

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

你应该取消评论它。

【讨论】:

  • 是的,收到了通知,但它是空的。 :(
  • 更改了通知接收器的某些部分,现在可以正常工作了,谢谢。 #thumbsup
猜你喜欢
  • 2023-03-26
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多