【发布时间】:2017-02-23 18:38:10
【问题描述】:
每当我的应用在后台收到推送时,它都会自动处理和显示。
每当应用程序处于前台时,都会使用我的GCMMessageHandler 类手动显示推送。
这是GCMMessageHandler的sn-p
@Override
public void onMessageReceived(String from, Bundle data) {
super.onMessageReceived(from, data);
MyNotification notification = new MyNotification(getApplicationContext(), data);
notification.handleNotification();
}
问题是每当我的应用在后台中时,推送通知会显示两次而不是一次(在这种情况下显然不会调用 onMessageReceived)。
我找到了问题所在,但我不知道如何解决。
这是在我的 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.codepath.gcmquickstart" />
</intent-filter>
</receiver>
<service
android:name=".helpers.GCMMessageHandler"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我注意到如果我删除以下行:
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
从这两个声明中,通知不再显示两次,但前台通知不再起作用(不再调用 onMessageReceived)。
如果我从接收器或服务中删除线路,它的行为完全相同。
知道如何正确执行此操作,以便当应用程序处于后台时我的通知将仅显示一次,而在前台时仍会调用 onMessageReceived?
【问题讨论】:
标签: android notifications google-cloud-messaging gcmlistenerservice