我正在使用 curl 使用 PHP 发送推送通知,结果似乎返回正常:
{"multicast_id":2345735671345346,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:13456457587969%375ed23445237ecd"},{"message_id":"0:12344526107806%375ed3439fd7ecd"}]}
所以我猜测消息已发送,问题出在BroadcastReceiver
子类上。
我的广播接收器代码:
package com.example.myexample.pushnotifications;
public class GCMBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
Log.d(TAG, "onReceive method executed properly");
//NotificationManager and NotificationCompat.Builder code to build a notification
}
}
然后我AndroidManifest.xml
的权限:
<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="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.pushnotifications.permission.C2D_MESSAGE" />
对于应用程序标签内的接收器:
<receiver
android:name="com.example.myexample.pushnotifications.GCMBroadcastReceiver"
android:enabled="true"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.myexample.pushnotifications" />
</intent-filter>
</receiver>
问题是广播接收器永远不会收到消息。