繁体   English   中英

当app在前台时,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()。

当应用程序处于后台并且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);

UPDATE

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

这是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消息:null。

应该注意的是,当示例客户端应用程序处于前台时,此方法在之前的情况下未被调用。

所以现在有3种可能性。

  • 我们使用通知有效负载发送下游消息的方式有问题或缺失。
  • 这种行为只是这样。
  • 一个bug的可能性。

请帮帮我。

所以有可能没有。 3“这种行为只是这样。”

以下是gcm-dev-support@google.com的回复:

这是为了向您确认,当应用程序在后台时,Android上的通知有效负载将仅显示在通知托盘上。 如果它位于前台并且您希望部署通知,则可以考虑在示例应用程序中以与此方法类似的方式部署通知。 它是从onMessageReceived()调用的。 希望这可以帮助。

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

添加变量BroadcastReceiver mRegistrationBroadcastReceiver; 到你的班级

private void setUpBroadcastReceiver() {

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

            //create notification
        }
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM