android - 如果 GCM 自动显示,如何处理 Android 中的通知点击

标签 android push-notification google-cloud-messaging

在 GCM 的最新更新中,我在 Android 中遇到了 2 个问题。根据 GCM,如果有效载荷包含“通知”属性,它将自动在托盘中显示通知。但是他们没有提到如何处理该通知的点击事件。如果负载仅包含数据属性,则调用 GCMListenerService 的“onMessageReceived”。但是,如果负载同时包含通知和数据属性,则不会调用该方法。知道如何解决吗?我还必须检查 iOS,看看它在那里的表现如何。

最佳答案

您需要在通知负载中设置一个 click_action。然后,当用户打开/点击通知时,您的应用中使用该操作声明的 Activity 将启动。

例如设置 click_action:OPEN_ACTIVITY_1,然后将以下 Intent 过滤器添加到所需的 Activity:

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

然后您可以使用 getIntent() 从 Activity 中的消息中提取数据,然后查看 intent extras。

请参阅此处的条目:https://developers.google.com/cloud-messaging/server-ref#notification-payload-support

关于android - 如果 GCM 自动显示,如何处理 Android 中的通知点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30785609/

相关文章:

android - findFragmentByTag() 始终返回 null - Android

javascript - 'setRemoteDescription'上执行 'RTCPeerConnection'失败 : Failed to set remote answer sdp

ios - 如何测试推送通知在我的应用程序中是否有效

windows - Windows 桌面中的 Web 推送通知

android - 应用程序在接收到 gcm 消息广播时没有被唤醒

php - 解析 JSON 数据时出错。 JSONException : End of input at character

android - 在 Android 中将 ImageView 移动到屏幕中心

ios - 通知触发错误。 swift

java - 使用 Back4App 发送 ParsePush 通知

android - 从 GCM 通知中获取数据