当应用程序处于前台时,GCM 3.0 通知负载不显示 android 通知

GCM 3.0 notification payload not showing android notification when app is in foreground

当我发送带有通知(无数据)负载的 gcm 消息时,只有当我的应用程序在后台时才会显示 android 通知。如果应用程序在前台,则不会显示 android 通知,但会使用 null "message".

调用 GcmListenerService 实现中的 onMessageReceived()

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

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

这是服务器端代码片段:

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);

更新

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

这是 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种可能。

请帮帮我。

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

将变量 BroadcastReceiver mRegistrationBroadcastReceiver; 添加到您的 class

private void setUpBroadcastReceiver() {

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

            //create notification
        }
    };
}

所以可能性不大。 3 "The behavior is like this only."

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

This is to confirm to you that the notification payload on Android will only show on the notification tray when the app is in the background. If it is in the foreground and you would like to deploy a notification, you could consider deploying notifications similarly to this method in the sample app. It is called from onMessageReceived(). Hope this helps.