我尝试在我的Android应用程序中放置GCM,我最终接受了“通知”,但它不是一个好的,只是一个像它的方形。
我不知道问题是在我身边(android部分)还是在另一边(php)
就我而言,我认为唯一的问题可能出在清单上:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlecloudmessaging8"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission android:name="com.example.googlecloudmessaging8.permission.C2D_MESSAGE"
android:protectionLevel="signature" ></permission>
<uses-permission android:name="com.example.googlecloudmessaging8.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<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>
<receiver
android:name=".GcmReceiver"
android:exported="true"
android:process=":remote"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.googlecloudmessaging8" />
</intent-filter>
</receiver>
<service android:name=".GcmMessageHandler"></service>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
GCMReceiver:
public class GcmReceiver extends WakefulBroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
GcmMessageHandler:
public class GcmMessageHandler extends IntentService{
String mes;
private Handler handler;
public GcmMessageHandler(){
super("GcmMessageHandler");
}
@Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent){
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Received : (" + messageType + ") " +extras.getString("title"));
GcmReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable(){
@Override
public void run() {
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG).show();
}
});
}
}
在gcm_test.php上,我看到了:
$aPushs[$iCpt]['message'] = 'message par defaut';
$aPushs[$iCpt]['link'] = 'http://www.google.fr/';
我只有一个黑色方块,而不是“消息相似”,有人知道为什么? (在另一台设备上它是一个矩形,也是空的)
答案 0 :(得分:1)
我认为您的代码与此类似: https://www.youtube.com/watch?v=Vd8bIkE29U4
请按照步骤进行操作。我试过了,它完美无缺。
另外,你可以在这里source code
引用我的github: