0

我正在使用 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>

问题是广播接收器永远不会收到消息。

4

1 回答 1

1

假设应用程序的包是com.example.myexample,需要进行以下更改:

<permission
    android:name="com.example.myexample.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.myexample.permission.C2D_MESSAGE" />

<intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <category android:name="com.example.myexample" />
</intent-filter>
于 2013-07-16T19:57:34.603 回答