gpt4 book ai didi

android - GCM : onMessage() from GCMIntentService is called very few times

转载 作者:行者123 更新时间:2023-11-29 21:56:28 25 4
gpt4 key购买 nike

我正在我的应用程序中实现 GCM。我已按照 developer.android.com 的 GCM 教程中给出的所有步骤进行操作

我的应用程序的构建目标指向 Goolge API 8(Android 2.2 版本)

我能够从 GCM 成功获取注册 ID,并将此 ID 传递到我的应用程序服务器。至此注册步骤执行成功。

现在,当我的应用程序服务器向我的设备发送 PUSH 消息时,服务器收到的消息为 SUCCESS=1 FAILURE=0, etc.,即服务器成功发送消息,但我的设备从未收到消息。

经过大量搜索,我了解到 GCM 在端口号 5228、5229 或 5230 上推送消息。

最初,我的设备和笔记本电脑被限制访问某些网站,但后来我被授予访问所有网站的所有权限,所以我猜这些端口号对我的设备是开放的。

所以我的问题是:极少数情况下,我收到来自 GCM 的 PUSH 消息。为什么谷歌在传递消息时如此不确定?可能是什么原因?

请查看我的以下代码并相应地指导我:

我在 list 中声明了以下内容:

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />

<permission
android:name="package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- 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" />
<uses-permission android:name="package.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<receiver
android:name="com.google.android.gcm.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="packageName" />
</intent-filter>
</receiver>
<receiver
android:name=".ReceiveBroadcast"
android:exported="false" >
<intent-filter>
<action android:name="GCM_RECEIVED_ACTION" />
</intent-filter>
</receiver>

<service
android:name=".GCMIntentService"
/>


/**
* @author Shrikant.
*
*/
public class GCMIntentService extends GCMBaseIntentService {

/**
* The Sender ID used for GCM.
*/
public static final String SENDER_ID = "myProjectID";

/**
* This field is used to call Web-Service for GCM.
*/
SendUserCredentialsGCM sendUserCredentialsGCM = null;

public GCMIntentService() {
super(SENDER_ID);
sendUserCredentialsGCM = new SendUserCredentialsGCM();
}

@Override
protected void onRegistered(Context arg0, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
sendUserCredentialsGCM.sendRegistrationID(registrationId);
}

@Override
protected void onUnregistered(Context context, String arg1) {
Log.i(TAG, "unregistered = " + arg1);
sendUserCredentialsGCM
.unregisterFromGCM(LoginActivity.API_OR_BROWSER_KEY);
}

@Override
protected void onMessage(Context context, Intent intent) {
Log.e("GCM MESSAGE", "Message Recieved!!!");
String message = intent.getStringExtra("message");
if (message == null) {
Log.e("NULL MESSAGE", "Message Not Recieved!!!");
} else {
Log.i(TAG, "new message= " + message);
sendGCMIntent(context, message);
}
}

private void sendGCMIntent(Context context, String message) {
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("GCM_RECEIVED_ACTION");
broadcastIntent.putExtra("gcm", message);
context.sendBroadcast(broadcastIntent);
}

@Override
protected void onError(Context context, String errorId) {
Log.e(TAG, "Received error: " + errorId);
Toast.makeText(context, "PUSH Notification failed.", Toast.LENGTH_LONG)
.show();
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
}

谢谢。

最佳答案

检查调试证书是否过期,如果过期则创建新证书并生成新 key 。

关于android - GCM : onMessage() from GCMIntentService is called very few times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099089/

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