我正在编写一个支持GCM的Android 4.0.3程序。
应用程序注册正常,从服务器发送消息似乎也有效(从Google获得“成功”并带有消息ID),但我的BroadcastReceiver上的onReceive
方法由于某种原因不会被解雇。
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jlieberman.nightwatch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="com.jlieberman.nightwatch.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jlieberman.nightwatch.NightWatch"
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=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.jlieberman.nightwatch" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
</application>
</manifest>
这是BroadcastReceiver
:
package com.jlieberman.nightwatch;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
知道造成这个问题的原因是什么吗? 谢谢!
答案 0 :(得分:4)
您的清单中有<uses-permission android:name="com.jlieberman.nightwatch.permission.C2D_MESSAGE" />
,但我看不到您在何处定义此权限。
你应该添加:
<permission android:name="com.jlieberman.nightwatch.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
答案 1 :(得分:1)
好请试试这个
import com.google.android.gms.gcm.GoogleCloudMessaging;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "pushnotification";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
WakeLocker.acquire(ctx);
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
intent.getExtras().toString());
} else {
sendNotification("Received: " + intent.getExtras().toString());
}
setResultCode(Activity.RESULT_OK);
WakeLocker.release();
}
}
对于wakelocker,我写了一堂课,就在这里,
WakeLocker.java
import android.content.Context;
import android.os.PowerManager;
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context context) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "WakeLock");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
尝试这个,如果它不起作用,请在这里帮助!!!!
答案 2 :(得分:0)
我最近遇到了同样的问题,并发现它是权限中的包名称错误。需要严格遵循本指南,使用主要包裹的名称,谷歌的例子适用于儒家思想。
将以下内容添加到您的应用程序的清单中:
The com.google.android.c2dm.permission.RECEIVE
The android.permission.INTERNET
The android.permission.GET_ACCOUNTS
The android.permission.WAKE_LOCK
An applicationPackage + ".permission.C2D_MESSAGE"
并在过滤器名称中:
A receiver for com.google.android.c2dm.intent.RECEIVE