【问题标题】:GCM 3.0 notification payload not showing android notification when app is in foreground当应用程序处于前台时,GCM 3.0 通知负载不显示 android 通知
【发布时间】:2015-12-01 10:28:01
【问题描述】:

当我发送带有通知(无数据)有效负载的 gcm 消息时,只有当我的应用程序在后台时才会显示 android 通知。如果应用程序在前台,则不会显示 android 通知,但 GcmListenerService 实现中的 onMessageReceived() 会以 null“消息”调用。

onMessageReceived() 不会在应用程序处于后台并且按预期显示 android 通知时调用。

这是预期的行为还是我错过了什么?如果需要任何客户端代码,请告诉我。

这里是服务器端代码sn-p:

Message.Builder messageBuilder = new Message.Builder();
messageBuilder.notification(new Notification.Builder("ic_launcher")
.clickAction("ScrHome1")
.title("Bling")
.body(blingNotification.getPayload().getValue())
.build());
Message message = messageBuilder.build();
sender.send(message, targetIdList, retries);

更新

我尝试在谷歌提供的示例应用程序中检查相同的行为,并发现它是相同的。那么应该是这样的吗?

这是 gcm 示例服务器端代码,我们在其中进行了细微更改以发送仅包含通知有效负载的消息。

public class GcmSender {

public static final String API_KEY = "AIaSyCMzWOageHbcX9yxNtfL6RygdbLT-7Ls";

@SuppressWarnings("unchecked")
public static void main(String[] args) {

    try {
        // Prepare JSON containing the GCM message content. What to send and where to send.
        JSONObject jGcmData = new JSONObject();
        JSONObject jData = new JSONObject();
        JSONObject jNotification = new JSONObject();
        jData.put("message", "hello");
        jNotification.put("body", "hi dev");
        jNotification.put("title", "POC");
        jNotification.put("icon", "ic_launcher");
        // Where to send GCM message.
        jGcmData.put("to",
                "eucb9MGv3g:APA91bGJWZjBET12nYuDrX8yiylPqt3uy87ThP2f4E9Nw89GOvbZkWSTFVPyQ8keTPQubWzpW_10-Aydqu04MD1GvzeTUAh6SoFk6qeXSUW0205h6sbQdTe74VZfMu8t2P9nrWOE");
        // What to send in GCM message.
        jGcmData.put("notification", jNotification);

        // Create connection to send GCM Message request.
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "key=" + API_KEY);
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);

        // Send GCM message content.
        OutputStream outputStream = conn.getOutputStream();
        outputStream.write(jGcmData.toString().getBytes());

        // Read GCM response.
        InputStream inputStream = conn.getInputStream();
        String resp = IOUtils.toString(inputStream);
        System.out.println(resp);
        System.out.println("Check your device/emulator for notification or logcat for "
                + "confirmation of the receipt of the GCM message.");
    } catch (IOException e) {
        System.out.println("Unable to send GCM message.");
        System.out.println("Please ensure that API_KEY has been replaced by the server "
                + "API key, and that the device's registration token is correct (if specified).");
        e.printStackTrace();
    }
}

}

观察结果是一样的,只要示例客户端应用程序在后台,就会显示 android 通知,而无需任何客户端代码干预,但只要示例客户端应用程序在前台,则不会显示 android 通知,而是 MyGcmListenerService.onMessageReceived(String from , Bundle data) 调用值为 From: 234552842207 Message: null。

需要注意的是,在之前的例子中,当示例客户端应用程序在前台时,这个方法没有被调用。

所以现在有 3 种可能性。

  • 我们发送带有通知负载的下游消息的方式有问题或缺失。
  • 行为只有这样。
  • 可能存在错误。

请帮帮我。

【问题讨论】:

  • 在这里尝试官方回购:github.com/google/gcm/tree/master/samples/android/gcm-demonotification 标签上
  • @bjiang 我在检查了官方仓库中的示例应用程序后更新了问题
  • 这是按预期工作的。当应用程序处于前台时,您将在 GcmListenerService 的 onReceived 回调中获取消息负载(包括通知)。

标签: android google-cloud-messaging


【解决方案1】:

所以不可能。 3 “行为只有这样。”

这是来自 gcm-dev-support@google.com 的回复:

这是为了向您确认,Android 上的通知负载仅在应用处于后台时才会显示在通知托盘上。如果它在前台并且您想要部署通知,您可以考虑在示例应用程序中部署类似于this method 的通知。它是从 onMessageReceived() 调用的。 希望这会有所帮助。

【讨论】:

    【解决方案2】:

    在您的聊天活动中创建如下所示的广播接收器并在 onCreate 函数中调用它。

    将变量 BroadcastReceiver mRegistrationBroadcastReceiver; 添加到您的

    private void setUpBroadcastReceiver() {
    
        mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
    
                //create notification
            }
        };
    }
    

    【讨论】:

    • 问题是关于 GCM 3.0。情况发生了变化。
    • 我已经完成了一个聊天应用程序并使用 gcm 3.0 交付给客户。请详细说明您的问题。我们无法想象您的代码会得出正确的结论。
    • gcm 消息有两种类型,一种带有通知负载,另一种带有数据负载。我的问题是关于带有通知有效负载的消息,它直接显示 Android 通知而无需客户端应用程序的干预。你用过吗?
    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多