我在接收从 GCM 发送的消息时遇到了很多麻烦。我的WakefulBroadcastReceiver
永远不会被解雇。我四处寻找很多帮助,但没有用。对此的任何帮助将不胜感激!
唤醒广播接收器
public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "in BroadcastReceiver");
ComponentName componentName = new ComponentName(context.getPackageName(), MyGcmListenerService.class.getName());
intent.setComponent(componentName);
startWakefulService(context, intent);
setResultCode(Activity.RESULT_OK);
}
}
意向服务
public class MyGcmListenerService extends IntentService {
public static final String TAG = MyGcmListenerService.class.getSimpleName();
public MyGcmListenerService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("TAG", "in HandleIntent");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
Log.e("Send error: ", extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
Log.e("Deleted messages: ",
extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
String msg = extras.getString("message");
String urlImg = extras.getString("image");
String title = extras.getString("title");
Log.e("GCM Mesage: ", "msg: " + msg + " title: " + title);
Toast.makeText(MyGcmListenerService.this, "msg: " + msg + " title: " + title, Toast.LENGTH_LONG).show();
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
}
显现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.purepush.chatdemo">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="org.purepush.chatdemo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="org.purepush.chatdemo.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChatActivity" />
<receiver
android:name=".service.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="org.purepush.chatdemo" />
</intent-filter>
</receiver>
<service
android:name=".service.MyGcmListenerService"/>
<service
android:name=".service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service android:name=".service.RegistrationIntentService" />
</application>
</manifest>