我现在已经在互联网上看了很长一段时间,但还没有找到答案。 我已经按照示例和教程进行了操作: https://developers.google.com/cloud-messaging/android/client
我看到有几个人在尝试发送"通知"时遇到问题。有效负载,但对于我的应用,它只是"数据"有效载荷。
gcmListner:
public class GCMPushReceiver extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d("gcmReceiver", "something received");
String message = data.getString("message");
sendNotificasjon(message);
}
}
清单:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="com.example.myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.premission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.myapp"/>
</intent-filter>
</receiver>
<service android:name=".GCMPushResiver" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
<service android:name=".GCMregistrationIntentService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
</application>
postman sendt:
{
"to":"SOME_TOLKEN_XXX",
"data": {
"message": "melding"
}
}
邮递员收到:
{
"multicast_id": 73503399375464XXXX,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [
{
"message_id": "0:1471781513806225%030a2fe3f9fdXXX"
}
]
}