GCM 3.0通知负载在应用程序前台时未显示Android通知

4
当我发送一个带有通知(无数据)负载的GCM消息时,如果我的应用在后台,则只显示Android通知。如果应用程序在前台,则不会显示Android通知,但是GcmListenerService实现中的onMessageReceived()将null“message”调用。
当应用程序处于后台时,并且按预期显示Android通知时,不会调用onMessageReceived()。
这是否是预期行为还是我漏了什么?请告诉我是否需要任何客户端代码。
以下是服务器端代码片段:
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。

需要注意的是,在示例客户端应用程序在前台运行时,此方法没有被调用。

因此,现在有三种可能性。

  • 我们发送带有通知有效负载的下行消息存在某些问题或缺失。
  • 这种行为是正常的。
  • 可能存在错误。

请在这里帮助我解决这个问题。


请在“通知”选项卡上尝试官方存储库:https://github.com/google/gcm/tree/master/samples/android/gcm-demo - bjiang
@bjiang,我在检查官方仓库的示例应用程序后更新了问题。 - binaryKarmic
2
这是按预期工作的。当应用程序在前台时,您将在GcmListenerService的onReceived回调中收到消息负载(包括通知)。 - Arthur Thompson
2个回答

1
所以可能性3是“行为只是这样。”
以下是gcm-dev-support@google.com的回复:
这是向您确认,在Android上的通知负载只会在应用程序处于后台时显示在通知栏中。如果它在前台并且您想部署通知,则可以考虑类似于示例应用程序中此方法的方式部署通知。它从onMessageReceived()调用。 希望这有所帮助。

-1
在您的聊天活动中创建一个广播接收器,如下所示,并在onCreate函数中调用它。
将变量BroadcastReceiver mRegistrationBroadcastReceiver;添加到您的中。
private void setUpBroadcastReceiver() {

    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            //create notification
        }
    };
}

问题涉及GCM 3.0。情况已经发生了变化。 - binaryKarmic
我已经完成了一个聊天应用程序,并使用gcm 3.0交付给客户。请详细说明您的问题。我们无法想象您的代码如何得出正确的结论。 - ramses1592
有两种类型的 GCM 消息,一种是带通知负载的,另一种是带数据负载的。我的问题是关于带通知负载的消息,它们可以直接显示 Android 通知,而无需客户端应用程序的干预。你使用过吗? - binaryKarmic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接