【发布时间】:2017-01-21 16:45:56
【问题描述】:
我从 Android Studio 的 Android 监视器收到了该错误。当我通过 GCM 在真实设备中发送推送通知时出现此错误,并且应用程序尚未启动或已被强制停止。昨天一切正常,今天根本不工作(仅当应用程序在后台或前台运行时才有效)。
我认为这可能是AndroidManifest 错误,但我厌倦了寻找问题并且找不到任何东西。
清单
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flagg327.guicomaipu">
...
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
<!--GOOGLE CLOUD MESSAGE-->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- for Gingerbread GSF backward compat -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.flagg327.guicomaipu" />
</intent-filter>
</receiver>
<service
android:name="com.flagg327.guicomaipu.gcm.RegistrationService"
android:exported="false" />
<service
android:name="com.flagg327.guicomaipu.gcm.TokenRefreshListenerService"
android:exported="false">
<intent-filter>
<action
android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.flagg327.guicomaipu.gcm.NotificacionsListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
</aplication>
<permission
android:name="com.flagg327.guicomaipu.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.flagg327.guicomaipu.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
TokenRefreshListenerService.java
注册“令牌”每天都会更新。这是因为,每个使用 GCM 的 Android 应用程序都必须有一个 InstanceIDListenerService 来管理这些更新。
public class TokenRefreshListenerService extends InstanceIDListenerService{
@Override
public void onTokenRefresh() {
// Launch the registration process.
Intent i = new Intent(this, RegistrationService.class);
startService(i);
}
}
NotificacionsListenerService.java
GCM 会自动显示推送通知,但前提是关联的应用具有 GCMListenerService
public class NotificacionsListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d("A", "onMessageReceived()");
// Do something
}
}
RegistrationService.java
GCM 使用注册卡(“令牌”)识别 Android 设备。我的应用应该能够从安装了它的每个 Android 设备上注册。
public class RegistrationService extends IntentService {
/**
* Constructor
*/
public RegistrationService() {
super("RegistrationService");
}
@Override
protected void onHandleIntent(Intent intent) {
// Generate or download the registration 'token'.
InstanceID myID = InstanceID.getInstance(this);
String registrationToken = null;
try {
// Get the registration 'token'.
registrationToken = myID.getToken(
getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE,
null
);
// Subscribe to a topic. The app is able now to receive notifications from this topic.
GcmPubSub subscription = GcmPubSub.getInstance(this);
subscription.subscribe(registrationToken, "/topics/guico_maipu_topic", null);
} catch (IOException e) {
e.printStackTrace();
}
Log.e("Registration Token", registrationToken);
}
}
错误
当我通过 python 发送推送通知时出现此错误。
09-13 21:21:44.800 1851-1851/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.flagg327.guicomaipu (has extras) }
昨天还在工作……有什么想法吗?比你花时间。
【问题讨论】:
-
基于此thread,当接收应用程序在设备上处于停止状态时会发生此问题(例如,通过使用设置中的强制停止)。只有在手动启动时才会再次开始接收消息。您也可以查看related SO post。
-
喜欢检查这个 ..stackoverflow.com/a/45810771/1993001
-
Here 这些步骤可以帮助您。
标签: android push-notification google-cloud-messaging