简体   繁体   English

当app在前台时,GCM 3.0通知有效负载不显示android通知

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

When i am sending a gcm message with notification(no data) payload, then the android notification is shown only if my app is in background. 当我发送带有通知(无数据)有效负载的gcm消息时,仅当我的应用程序处于后台时才显示android通知。 If the app is in foreground then no android notification is shown but the onMessageReceived() in GcmListenerService implementation is called with null "message". 如果应用程序位于前台,则不显示任何android通知,但使用null“message”调用GcmListenerService实现中的onMessageReceived()。

onMessageReceived() is not called when the app is in background and android notification is shown as expected. 当应用程序处于后台并且Android通知按预期显示时,不会调用onMessageReceived()。

Is this the expected behavior or am i missing something? 这是预期的行为还是我错过了什么? Please let me know in case if any client side code is required. 如果需要任何客户端代码,请告诉我。

Here is the server side code snippet : 这是服务器端代码段:

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 UPDATE

I tried to check the same behavior in sample app provided b google and found that its same. 我试图在示例应用程序中检查相同的行为,提供了谷歌,并发现它相同。 So is it supposed to be like that ? 它应该是那样的吗?

Here is the gcm sample server side code in which we have done minor changes to send a message with notification only payload. 这是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();
    }
}

} }

The observation is same, whenever the sample client app is in background the android notification is shown without any client code intervention , but whenever the sample client app is in foreground the android notification is not shown but the MyGcmListenerService.onMessageReceived(String from, Bundle data) is called with values as From: 234552842207 Message: null. 观察结果是相同的,每当示例客户端应用程序处于后台时,都会显示android通知而没有任何客户端代码干预,但是每当示例客户端应用程序处于前台时,都不显示android通知,而是显示MyGcmListenerService.onMessageReceived(String from,Bundle data)调用值为From:234552842207消息:null。

It should be noted that this method wasn`t called in previous case when the sample client app was in foreground. 应该注意的是,当示例客户端应用程序处于前台时,此方法在之前的情况下未被调用。

So now there are 3 possibilities. 所以现在有3种可能性。

  • There is something wrong or missing in the way we are sending downstream message with notification payload. 我们使用通知有效负载发送下游消息的方式有问题或缺失。
  • The behavior is like this only. 这种行为只是这样。
  • Possibility of a bug. 一个bug的可能性。

Please help me out here. 请帮帮我。

So it was possibility no. 所以有可能没有。 3 "The behavior is like this only." 3“这种行为只是这样。”

Here is the reply from gcm-dev-support@google.com : 以下是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. 这是为了向您确认,当应用程序在后台时,Android上的通知有效负载将仅显示在通知托盘上。 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(). 它是从onMessageReceived()调用的。 Hope this helps. 希望这可以帮助。

In your Chat activity create a broadcast receiver like below and call it in the onCreate function. 在您的聊天活动中,创建如下所示的广播接收器,并在onCreate函数中调用它。

Add variable BroadcastReceiver mRegistrationBroadcastReceiver; 添加变量BroadcastReceiver mRegistrationBroadcastReceiver; to your class 到你的班级

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