GCM广播接收器未触发

时间:2016-04-13 08:05:05

标签: java php android google-cloud-messaging

我在这个网站上看过几个问题,似乎没有人能够回答我的问题所以希望有人可以帮忙解决这个问题。目前由于某些原因,当我从PHP网站发送消息时,我的应用程序没有收到消息。我已经尝试将Log.d放在代码行中,但它也没有出现。有人可以帮助看看是否出了什么问题?

[更新]这是我从php网站发送的结果 { “multicast_id”:5610818169388245295, “成功”:1, “失败”:0, “canonical_ids”:0 “结果”:[{ “MESSAGE_ID”: “0:1460536959734990%40ad5721f9fd7ecd”}]}

我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.amosang.pushtest" >

<!-- GCM Permissions - Start here  -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
    android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.amosang.pushtest.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/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>
    <activity
        android:name=".HomeActivity"
        android:label="@string/title_activity_home" >
    </activity>
</application>

<receiver
    android:name=".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.amosang.pushtest" />
    </intent-filter>
</receiver>

<service android:name=".GCMNotificationIntentService">
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
</service>

</manifest>

我的广播接收器

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(), GCMNotificationIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    Log.d("TESTTEST", "TEST");
    setResultCode(Activity.RESULT_OK);
}
}

我的意向服务

public class GCMNotificationIntentService extends IntentService{


//set ID for the notification, so it can be updated
public static final int notifyID = 9001;
NotificationCompat.Builder builder;
public GCMNotificationIntentService() {
    super("GCM Intent Service");
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);
    Log.d("RECEIVEDMESSAGE", messageType);

    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
                .equals(messageType)) {
            sendNotification("Send error: " + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
                .equals(messageType)) {
            sendNotification("Deleted messages on server: "
                    + extras.toString());
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                .equals(messageType)) {
            sendNotification("Message Received from Google GCM Server:nn"
                    + extras.get(AppConstants.MSG_KEY));
        }
    }

    GCMBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String msg){

    Intent resultIntent = new Intent(this, HomeActivity.class);
    Log.d("RECEIVEDPT2",msg);
    resultIntent.putExtra("msg", msg);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder mNotifyBuilder;
    NotificationManager mNotificationManager;

    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyBuilder = new NotificationCompat.Builder(this).setContentTitle("Alert")
            .setContentTitle("You've received a new message");

    //Set pending intent
    mNotifyBuilder.setContentIntent(resultPendingIntent);

    //Set vibration
    int defaults = 0;
    defaults = defaults | Notification.DEFAULT_LIGHTS;
    defaults = defaults | Notification.DEFAULT_VIBRATE;
    defaults = defaults | Notification.DEFAULT_SOUND;

    mNotifyBuilder.setDefaults(defaults);
    // Set the content for Notification
    mNotifyBuilder.setContentText("You have new notifications");
    // Set autocancel
    mNotifyBuilder.setAutoCancel(true);
    // Post a notification
    mNotificationManager.notify(notifyID, mNotifyBuilder.build());

}
}

请帮忙。

0 个答案:

没有答案