2

当应用程序在 Android 设备的前台时,我无法获取推送通知。一旦我将应用程序置于后台,一切顺利。

这是我用来发送 de 通知的 java 代码:

        HttpClient client = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(URL_SERVER);
    List<NameValuePair> arguments = new ArrayList<>();   
    arguments.add(new BasicNameValuePair("token", TOKEN));        
    arguments.add(new BasicNameValuePair("device", codigoApp));      
    arguments.add(new BasicNameValuePair("type", "1"));        
    arguments.add(new BasicNameValuePair("body", ip));                    
    arguments.add(new BasicNameValuePair("auth", GOOGLE_AUTH));        
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(arguments));
        HttpResponse response = client.execute(httpPost);   
        String result = EntityUtils.toString(response.getEntity());
        System.out.println(result);
    } catch (IOException ex) {
        Logger.getLogger(NotificaReview.class.getName()).log(Level.SEVERE, null, ex);
    }

这是应用程序中的代码:

    public void start() {
    if(current != null){
        current.show();
        return;
    }
    if (Push.getPushKey() != null)
        devicePush = Push.getPushKey(); 
    else
        Display.getInstance().registerPush(); 
    Form inicioGUI = new InicioGUI(devicePush);
    inicioGUI.show();
}

public void stop() {
    current = getCurrentForm();
    if(current instanceof Dialog) {
        ((Dialog)current).dispose();
        current = getCurrentForm();
    }
}

public void destroy() {
}

@Override
public void push(String value) {
  ToastBar.showMessage("Archivo recibido correctamente con IP" + value, FontImage.MATERIAL_INFO);
}

@Override
public void registeredForPush(String deviceId) {
    devicePush = deviceId;
}

@Override
public void pushRegistrationError(String error, int errorCode) {

}

ToastBar 仅在我在后台接收推送后将应用程序置于前台时显示。如果应用程序处于活动状态,则永远不会调用 Push 回调。

有任何想法吗?

4

1 回答 1

1

我需要回答我在问题中的评论,这可能有助于解释这个问题。我将根据问题的更新编辑此答案。

与此同时,我在代码中看到了几个问题。请参阅下面我突出显示的评论/修复:

public void start() {
    if(current != null){
        current.show();
        return;
    }
    // don't check the push key, always register the device and 
    // always do it in a callSerially as it might trigger a prompt
    callSerially(() -> registerPush()); 
    Form inicioGUI = new InicioGUI(Push.getPushKey());
    inicioGUI.show();
}

@Override
public void push(String value) {
  ToastBar.showMessage("Archivo recibido correctamente con IP" + value, FontImage.MATERIAL_INFO);
}

@Override
public void registeredForPush(String deviceId) {
    // deviceId is the native push key you need to use the actual 
    // push key value never device ID
    devicePush = Push.getPushKey();
}

@Override
public void pushRegistrationError(String error, int errorCode) {
    // you might have gotten a push error which might have explained the
    // cause of the problem
    Log.p("Push error " + errorCode + ":" + error);
    Log.sendLogAsync();
}
于 2019-03-04T03:13:21.283 回答