gpt4 book ai didi

java - 自定义 FCM 广播接收器未在一些设备上使用,但可在其他设备上使用

转载 作者:行者123 更新时间:2023-11-30 03:46:54 25 4
gpt4 key购买 nike

我创建了一个应用程序,它使用 GCM 从我们的服务器接收通知,我使用的是 Google 的 gcm.jar,并有一个自定义的 GCMIntentService 类来处理它。我的问题是,在调用 GCMRegistrar.register() 时,实际上只有一部手机从 GCM 获得响应(或者更可能正确路由响应)。

我看到的是 GCMRegistrar(来自 gcm 库)正确地将我的自定义广播接收器设置为 nexus 3 手机上的重试接收器,这是唯一一个可以工作的,但在其他手机上不起作用。这让我相信我可能会收到来自 GCM 的响应,但它不会在其他三部手机上得到处理(详情如下)。

GCMRegistrar Setting the name of retry receiver class to <My_application_package>.CustomGCMBroadcastReceiver

它们都有允许与 GCM 通信的数据连接,它们都有一个活跃的 gmail 帐户(它们都有 playstore 工作)。我还在它们上运行了 Google 的 GCM 演示,没有出现任何问题。

注册码:

 //This is almost identical to how Google's GCM demo does it.
private void registerOnGCM(){
checkNotNull(SERVER_URL, "SERVER_URL"); //What it says on the tin
checkNotNull(SENDER_ID, "SENDER_ID");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
/*
registerReceiver(mHandleMessageReceiver,
new IntentFilter(HANDLE_MESSAGE));
*/
final String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
if (regId.equals("")) {
// Automatically registers application on startup.
GCMRegistrar.register(getApplicationContext(), SENDER_ID);
}
else {
// Device is already registered on GCM, check server.
if (GCMRegistrar.isRegisteredOnServer(getApplicationContext())) {
// Skips registration.
Log.i(TAG, "Already registered");
} else {
// Try to register again, but not in the UI thread.
// It's also necessary to cancel the thread onDestroy(),
// hence the use of AsyncTask instead of a raw thread.
final Context context = this;
mRegisterTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
boolean registered = ServerUtilities.register(context, regId);

if (!registered) {
GCMRegistrar.unregister(context);
}
return null;
}

@Override
protected void onPostExecute(Void result) {
mRegisterTask = null;
}

};
mRegisterTask.execute(null, null, null);
}
}

CustomGCMBroadcastReceiver 的代码不重要,GCMIntentService 也不重要,因为它们都没有被调用过。问题不在于他们。

我正在测试的设备:

  • 运行 CyanogenMod 7 的 HTC Desire GSM
  • 运行 Android 4.1.1 的 Google nexus 3(应用程序适用于此)
  • 运行 Android 4.0.1 的摩托罗拉 T910
  • 运行 Android 4.2.6 的三星 Galaxy mini

list :

<uses-sdk  android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- App has permission to read/write files on sd card. Used for RSS document -->
<uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- GCM connects to Google Services. -->
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<permission
android:name="<My_package_name>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission
android:name="<My_package_name>.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />



<application
android:allowBackup="true"
android:theme="@style/AppTheme"
android:label="@string/app_name">
<activity
android:name="<My_package_name>.MasterActivity"
android:screenOrientation="portrait">
</activity>

<activity android:name="<My_package_name>.WebActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!--
BroadcastReceiver that will receive intents from GCM
services and handle them to the custom IntentService.

The com.google.android.c2dm.permission.SEND permission is necessary
so only GCM services can send data messages for the app.
-->
<receiver
android:name="<My_package_name>.CustomGCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="<My_package_name>" />
</intent-filter>
</receiver>

<!--
Application-specific subclass of GCMBaseIntentService that will
handle received messages.

By default, it must be named .GCMIntentService, unless the
application uses a custom BroadcastReceiver that redefines its name.
-->
<service android:name="<My_package_name>.GCMIntentService"
android:enabled="true"/>
</application>

可能是我忘记添加的内容。让我知道。

欢迎所有想法。

-德累斯顿先生

最佳答案

好吧,它似乎已经通过清理、重建然后将其部署到其他设备来自行修复。

为什么从所有手机上删除所有痕迹,然后在它们上以 Debug模式运行没有用,除了在一部手机上我不知道。

关于java - 自定义 FCM 广播接收器未在一些设备上使用,但可在其他设备上使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14854531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com