gpt4 book ai didi

android - 接收从 Google Cloud Messaging 发送的消息 - Android

转载 作者:行者123 更新时间:2023-11-30 01:36:05 25 4
gpt4 key购买 nike

我在接收 GCM 发送的消息时遇到了很多麻烦。我的 WakefulBroadcastReceiver 永远不会被解雇。我四处寻找了很多帮助,但没有用。在这方面的任何帮助将不胜感激!

WakefulBroadcastReceiver

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("TAG", "in BroadcastReceiver");
ComponentName componentName = new ComponentName(context.getPackageName(), MyGcmListenerService.class.getName());
intent.setComponent(componentName);
startWakefulService(context, intent);
setResultCode(Activity.RESULT_OK);
}
}

Intent 服务

public class MyGcmListenerService extends IntentService {

public static final String TAG = MyGcmListenerService.class.getSimpleName();

public MyGcmListenerService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
Log.e("TAG", "in HandleIntent");
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);

if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType)) {
Log.e("Send error: ", extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType)) {
Log.e("Deleted messages: ",
extras.toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType)) {
for (int i=0; i<5; i++) {
Log.i(TAG, "Working... " + (i+1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
String msg = extras.getString("message");
String urlImg = extras.getString("image");
String title = extras.getString("title");
Log.e("GCM Mesage: ", "msg: " + msg + " title: " + title);
Toast.makeText(MyGcmListenerService.this, "msg: " + msg + " title: " + title, Toast.LENGTH_LONG).show();
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
}

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.purepush.chatdemo">


<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
android:name="org.purepush.chatdemo.permission.C2D_MESSAGE"
android:protectionLevel="signature" />

<uses-permission android:name="org.purepush.chatdemo.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">


<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ChatActivity" />

<receiver
android:name=".service.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="org.purepush.chatdemo" />
</intent-filter>
</receiver>

<service
android:name=".service.MyGcmListenerService"/>
<service
android:name=".service.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service android:name=".service.RegistrationIntentService" />
</application>

</manifest>

最佳答案

好的.. 所以这里有几件事需要解决。首先是扩展 GcmListenerService(它实际上继承自 IntentService,但会为您完成其余工作),然后使用 Intent 过滤器声明它

<action android:name="com.google.android.c2dm.intent.RECEIVE" />

此外,如果可能,尽量坚持使用 Google 提供的 GcmReceiver,我认为它继承自 WakefulBroadcastReceiver ( https://developers.google.com/android/reference/com/google/android/gms/gcm/GcmReceiver)。

您可以在 https://github.com/ucsunil/android-gcm-practice/tree/master/android-gcm-chat 找到我正在处理的一个非常简单的示例- 一个聊天应用程序(非常类似于我相信你正在尝试做但后来停止了它。我记得查看日志并且我记得它会正确接收消息并且你可以用它作为例子。我使用了 Gcm提供的服务和接收器。

`

关于android - 接收从 Google Cloud Messaging 发送的消息 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099519/

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