fla*_*327 23 android push-notification google-cloud-messaging
我从Android Studio的Android Monitor收到了这个错误.当我通过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"/>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
NotificacionsListenerService.java
GCM会自动显示推送通知,但前提是关联的应用程序具有GCMListenerService
public class NotificacionsListenerService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d("A", "onMessageReceived()");
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
错误
当我通过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) }
Run Code Online (Sandbox Code Playgroud)
昨天工作......有什么想法吗?比你的时间.
nig*_*ann 11
我得到了同样的错误
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.XXX.XXX (has extras) }
Run Code Online (Sandbox Code Playgroud)
杀死应用程序后.
经过一些研究,我意识到,这只是一个"调试问题".即使应用程序已关闭,"已签名的APK"也能正确处理广播.
希望能帮助到你!
由于没有答案提供官方链接,因此我决定向火力基地团队询问并从他们那里获得官方答案。
强制停止或终止您的应用程序时,您似乎遇到了问题。实际上,这按预期工作。Android框架建议,在没有明确的用户交互的情况下,不应启动已停止(即从“设置”中终止/强制停止”)的应用程序。FCM遵循此建议,因此其服务也不会启动。这也意味着当应用程序处于“ killed”状态时,将不会接收消息(不会调用FirebaseMessagingService)。这是一些有用的链接,因此您可以更好地了解此主题:https : //developer.android.com/about/versions/android-3.1#launchcontrols https://github.com/firebase/quickstart-android/issues / 368#issuecomment-343567506
总共:
注意:即使问题与GCM有关,但FCM在问题标题中会引发完全相同的错误。
所以......我解决了这个问题.问题是如果应用程序强制关闭或者自设备启动以来从未打开应用程序,则设备未注册接收GCM.解决方案很简单,在手机启动时注册设备.为此,我实现了一个BroadcastReceiver并在其中启动了一个进程注册.
修改:
添加到AndroidManifest
<receiver android:name="com.flagg327.guicomaipu.gcm.OnBootBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
OnBootBroadcastReceiver.java
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.flagg327.guicomaipu.gcm.RegistrationService");
i.setClass(context, RegistrationService.class);
context.startService(i);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在启动时,设备将注册到GCM服务器,并能够从我的服务器接收任何推送通知.我希望它有用.
在模拟器上安装我的应用程序时,我也遇到了这个问题。我仅使用 FCM 数据消息,因为我需要自己处理消息(也有静默推送)。
但是当我测试后台时没有任何反应。我也观察到你的错误
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) }
Run Code Online (Sandbox Code Playgroud)
我读了很多相关内容,因为我不敢相信当应用程序被终止时 FCM 不起作用,这会破坏推送通知的整个目的。我还观察了收到我的消息的场景,即使应用程序被杀死,但我不知道为什么会时不时地发生这种情况。
在阅读了 github 上的 firebase issues 后,我停在了一个非常了不起的答案上:
经过一些测试后,似乎只有当我使用 Android Studio 启动应用程序时才会出现 stop=true 。如果我手动启动应用程序或使用 adb shell am start -n 然后终止它(从最近的内容中滑动),则状态为 stop=false 并且推送通知工作得很好!
https://github.com/firebase/quickstart-android/issues/822#issuecomment-611567389
因此,当您仍然遇到此问题时,请尝试以下操作:
在深入研究了发生这种情况的原因之后,如果您从Android Studio中终止了该应用程序,则似乎发生了这种情况。
如果您从启动器中打开应用程序并将其轻扫开,您的应用程序仍会收到通知。
(在具有FCM而非GCM的OnePlus One上测试)
| 归档时间: |
|
| 查看次数: |
10405 次 |
| 最近记录: |