gpt4 book ai didi

google-cloud-messaging - GCM 适用于 4.1 但不适用于 2.3 android 版本

转载 作者:行者123 更新时间:2023-12-02 00:13:05 25 4
gpt4 key购买 nike

我在使用 GCM 时遇到问题,它在 Nexus 7 上工作得很好,但是当我在任何带有 Gingerbread 版本的设备上运行它时,永远不会调用 onRegistered 方法。

请参阅下面我的代码实现:

GMCIntentService



<pre><code>public class GCMIntentService extends GCMBaseIntentService {
private static final String TAG = "GCMIntentService";

private RestHelper restRegisterGCM;
private String userRegisterGCMUrl = "User/SetGcm";

public GCMIntentService() {
super(AppSettings.SENDER_ID);
}

/**
* Method called on device registered
**/
@Override
protected void onRegistered(Context context, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
// Util.displayMessage(context, "Your device registred with GCM");
if (!GCMRegistrar.isRegisteredOnServer(this)) {

restRegisterGCM = new RestHelper(userRegisterGCMUrl, RequestMethod.POST, context);
restRegisterGCM.setHeader("UserName", AppSettings.getInstance().getUsername(context));
restRegisterGCM.setHeader("Password", AppSettings.getInstance().getPassword(context));
restRegisterGCM.setParameter("regId", registrationId);
restRegisterGCM.execute();

}
}

/**
* Method called on device un registred
* */
@Override
protected void onUnregistered(Context context, String registrationId) {
Log.i(TAG, "Device unregistered");
}

/**
* Method called on Receiving a new message
* */
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String message = intent.getExtras().getString("Message");
// notifies user
generateNotification(context, message);
}

/**
* Method called on receiving a deleted message
* */
@Override
protected void onDeletedMessages(Context context, int total) {
Log.i(TAG, "Received deleted messages notification");
}

/**
* Method called on Error
* */
@Override
public void onError(Context context, String errorId) {
Log.i(TAG, "Received error: " + errorId);
Toast.makeText(context, getString(R.string.gcm_error, errorId), Toast.LENGTH_SHORT).show();
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
// log message
Log.i(TAG, "Received recoverable error: " + errorId);
Toast.makeText(context, getString(R.string.gcm_recoverable_error, errorId), Toast.LENGTH_SHORT).show();
return super.onRecoverableError(context, errorId);
}
</code></pre>

GMC注册方式


private void registerGCM() {



<pre><code> // Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
Boolean accountExists = false;

AccountManager am = AccountManager.get(getApplicationContext());
Account[] accounts = am.getAccounts();

for (Account account : accounts) {

if (account.type.equals("com.google")) {
accountExists = true;
break;
}

}
if (accountExists) {

// Get GCM registration id
String regId = GCMRegistrar.getRegistrationId(this);

// Check if regid already presents
if (regId.equals("")) {
// Registration is not present, register now with GCM
GCMRegistrar.register(this, AppSettings.SENDER_ID);

} else {
// Device is already registered on GCM
if (!GCMRegistrar.isRegisteredOnServer(this)) {

restRegisterGCM = new RestHelper(userRegisterGCMUrl, RequestMethod.POST, EvadoFilipActivity.this);
restRegisterGCM.setHeader("UserName", AppSettings.getInstance().getUsername(EvadoFilipActivity.this));
restRegisterGCM.setHeader("Password", AppSettings.getInstance().getPassword(EvadoFilipActivity.this));
restRegisterGCM.setParameter("regId", regId);
restRegisterGCM.setPostExecuteMethod(2);
restRegisterGCM.execute();

}
}
} else
Toast.makeText(this, R.string.gcm_google_account_missing, Toast.LENGTH_SHORT).show();

}
</code></pre>

更新:

我重命名了包但忘记在我的类中更改它:


public class GCMBroadcastReceiver extends com.google.android.gcm.GCMBroadcastReceiver{<br/>
@Override
protected String getGCMIntentServiceClassName(Context context) {



<pre><code> return "com.mypackage.services.GCMIntentService";
}
}
</code></pre>

最佳答案

我遇到了同样的问题。我的代码可以在 nexus4(Kitkat) 上运行,但无法从 appln 服务器(通过 gcm 服务器)获得通知。@Fr0g 对于低于 4.0.4 的版本是正确的,你应该确保你在你的设备上设置了你的谷歌帐户,以便 gcm 工作。我在我的 galaxy ace(2.3.4) 上有谷歌帐户,但我犯的错误是我在我的 galaxy ace 中的帐户和同步设置是“关闭”。当我打开它并运行我的代码时,我收到了通知。

关于google-cloud-messaging - GCM 适用于 4.1 但不适用于 2.3 android 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563131/

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