gpt4 book ai didi

android - GcmListenerService onMessageReceived 仅在应用程序处于前台时调用

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

我有以下 list -

<service
android:name=".RegistrationIntentService"
android:exported="false" />

<service
android:name=".TestGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.test" />
</intent-filter>
</service>

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

<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.example.test" />
</intent-filter>
</receiver>

并遵循 onMessageReceived 方法 -

@Override
public void onMessageReceived(String from, Bundle data) {
ObjectMapper objectMapper = new ObjectMapper();
Message message;
try {
message = objectMapper.readValue(String.valueOf(data.get("message")), Message.class);
MessageNotification.notify(this, message, 1);
} catch (IOException e) {
e.printStackTrace();
}
}

发生的事情是,onMessageReceived 仅在应用程序处于前台时才被调用。否则它不会被调用并且不会显示通知。当我打开应用程序时,所有通知都会立即显示。

如果锁定并执行工作,GcmListenereService 不会唤醒设备吗?如何解决这个问题?

最佳答案

Google 搜索并设法在 github 上找到这个线程,它与您关注的问题几乎相同,其中 onMessageReceived() is only called when app is on foreground .原因是因为只发送了通知消息。根据线程中包含的描述:

Notification Messages - these are intended to generate a notification with no intermediate processing by the application. They only hit onMessageReceived if the app is running.

Data Messages - these are intended to silently pass data to the app's messaging service. They hit onMessageReceived even if the app is in the background. The service may then choose to generate a notification using the normal system notification APIs, or it may choose to handle the message silently.

我认为您正在发送通知消息,同时期待数据消息的行为开始。

这也是 SO post of Shiprack在github线程中提到。希望这可以帮助。祝你好运。

关于android - GcmListenerService onMessageReceived 仅在应用程序处于前台时调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36477182/

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