[英]GCM Notification nothing happens after sending via PHP
Can anyone see what am doing wrong? 谁能看到做错了什么? Am trying to use GCM to send push notification to an android device 我正在尝试使用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;
Nothing happens it just returns GCM sent successfully and following result what could be the problem? 什么都没有发生,它只是返回成功发送的GCM,并且跟随结果可能是什么问题?
I used both the Server Application Key and Browser application still no result or is it the registration id or do i need the device id(Though i know GCM no longer uses device ids)? 我使用服务器应用程序密钥和浏览器应用程序仍然没有结果,或者是注册ID还是我需要设备ID(尽管我知道GCM不再使用设备ID)? But this is how my registration id looks like 但这是我的注册ID的样子
APA91bFGOnPlE57RBK-dzZDqar__j8pdT7ZOhjHgfJfRdCwGQyWi_vcsxgvs4jtibRg3TbNmPgD_PrQLEIysEtSpzV3iRFEBbbeCTlR_0P9Wvl6JeQwtmud97DSmBFFPyrjNdX3a6AV9hZrylO8gyzcqGkAGZoZH0A APA91bFGOnPlE57RBK-dzZDqar__j8pdT7ZOhjHgfJfRdCwGQyWi_vcsxgvs4jtibRg3TbNmPgD_PrQLEIysEtSpzV3iRFEBbbeCTlR_0P9Wvyr6DSeZA7A9A7A9A9B1CB1M
and i get this as a result 结果我得到了
{"multicast_id":7629022022432875138,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1403458472513342%2a7e323af9fd7ecd"}]} {“ multicast_id”:7629022022432875138,“ success”:1,“ failure”:0,“ canonical_ids”:0,“ results”:[{“ message_id”:“ 0:1403458472513342%2a7e323af9fd7ecd”}]}
My GCM Broadcast Reciever 我的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");
}
}
My GCM BroadCast Intent Notification Service Reciever 我的GCM BroadCast意向通知服务接收者
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.");
}
}
The Manifest 清单
<?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>
NOTE: 注意:
I use blue stacks to test my app, could that be the problem cause i can't seem to debug while using bluestacks and the GenyMotion i use does not have google play services enabled for me to test and see the bugs 我使用蓝栈测试我的应用程序,这可能是问题所在,因为使用蓝栈时我似乎无法调试,而我使用的GenyMotion没有启用Google Play服务供我测试并查看错误
The only problem I can see is this commented line in your manifest : 我能看到的唯一问题是清单中的以下注释行:
<!-- uses-permission android:name="example.gbaalert.permission.C2D_MESSAGE" / -->
You should un-comment it. 您应该取消评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.