Android 接收并显示双重 GCM 推送通知
Android receives and displays double GCM push notifications
只要我的应用在后台收到推送,它就会自动处理并显示。
只要应用程序在前台,就会使用我的 GCMMessageHandler
class.
手动显示推送
这是GCMMessageHandler
的片段
@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 吗?
根据 GCM 文档的 Canonical IDs 部分,指出 "If a bug in the client app triggers multiple registrations for the same device, it can be hard to reconcile state and the client app might end up with duplicate messages."
要使用 Canonical ID,此 SO question 可以帮助您了解如何使用 GCM 实现它。
有关更多信息,请查看这些 SO 问题是否对您有帮助。
GCM notification duplication issue. Received same message many times
App receives duplicate notification using GCM after reinstalling
只要我的应用在后台收到推送,它就会自动处理并显示。
只要应用程序在前台,就会使用我的 GCMMessageHandler
class.
这是GCMMessageHandler
@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 吗?
根据 GCM 文档的 Canonical IDs 部分,指出 "If a bug in the client app triggers multiple registrations for the same device, it can be hard to reconcile state and the client app might end up with duplicate messages."
要使用 Canonical ID,此 SO question 可以帮助您了解如何使用 GCM 实现它。
有关更多信息,请查看这些 SO 问题是否对您有帮助。
GCM notification duplication issue. Received same message many times
App receives duplicate notification using GCM after reinstalling