gpt4 book ai didi

android - 关闭应用程序时不会调用 GCMListenerService

转载 作者:行者123 更新时间:2023-11-29 01:19:54 28 4
gpt4 key购买 nike

在 AndroidManifest.xml 中我定义了以下组件

<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="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.admin.freddyspeaks" />
</intent-filter>
</receiver>

<service
android:name="com.admin.services.NodeGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.admin.services.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="com.admin.services.RegistrationIntentService"
android:exported="false" />

NodeGcmListenerService.java

class NodeGcmListenerService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.d("", "From: " + from);
Log.d("", "Message: " + message);
//Set data
}

}

MyInstanceIDListenerService.java

public class MyInstanceIDListenerService extends InstanceIDListenerService {

@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}

这是RegistrationIntentService.java

public class RegistrationIntentService extends IntentService {

private static final String TAG = "RegIntentService";

public RegistrationIntentService() {
super(TAG);
}

@Override
public void onHandleIntent(Intent intent) {
try {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
sendRegistrationToServer(token);
}
catch(IOException e) {
Log.e(TAG,"Unable to register device",e);
}
}
}

当我从服务器发送消息并且应用被强制关闭时,NodeGcmListenerService onMessage 方法没有被调用

最佳答案

GCMListenerService doesnt get called when app is force closed

当应用程序被强制关闭时,它的字面意思是。无法保证正常的生命周期调用。您的应用程序行为不当,现在立即被击落。系统将清理残留物,但您的代码无事可做。

PS:崩溃后检查堆栈跟踪。

关于android - 关闭应用程序时不会调用 GCMListenerService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37496827/

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