首页 文章

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

提问于
浏览
4

当我发送带有通知(无数据)有效负载的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种可能性 .

  • 我们发送带有通知有效负载的下游消息的方式有问题或缺失 .

  • 这种行为只是这样 .

  • 错误的可能性 .

请帮帮我 .

2 回答

  • 1

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

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

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

  • -1

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

    将变量 BroadcastReceiver mRegistrationBroadcastReceiver; 添加到 class

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

相关问题