本文介绍了GCM 3.0通知负载在应用程序处于前景时不显示android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发送带有通知(无数据)有效载荷的gcm消息时,只有当我的应用程序处于后台时,才会显示android通知。如果应用程序在前台,那么没有显示android通知,但是在GcmListenerService实现中的onMessageReceived()用nullmessage来调用。

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

更新

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



以下是gcm示例服务器端代码,我们在该代码中只做了很小的更改以发送仅包含通知负载的消息。 p>

  public class GcmSender {

public static final String API_KEY =AIaSyCMzWOageHbcX9yxNtfL6RygdbLT-7Ls;

@SuppressWarnings(unchecked)
public static void main(String [] args){

try {
//准备包含GCM消息内容。发送内容和发送地点。
JSONObject jGcmData = new JSONObject();
JSONObject jData = new JSONObject();
JSONObject jNotification = new JSONObject();
jData.put(message,hello);
jNotification.put(body,hi dev dev);
jNotification.put(title,POC);
jNotification.put(icon,ic_launcher);
//在哪里发送GCM消息。
jGcmData.put(to,
eucb9MGv3g:APA91bGJWZjBET12nYuDrX8yiylPqt3uy87ThP2f4E9Nw89GOvbZkWSTFVPyQ8keTPQubWzpW_10-Aydqu04MD1GvzeTUAh6SoFk6qeXSUW0205h6sbQdTe74VZfMu8t2P9nrWOE);
//在GCM消息中发送什么。
jGcmData.put(notification,jNotification);

//创建连接发送GCM消息请求。
网址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);

//发送GCM信息内容。
OutputStream outputStream = conn.getOutputStream();
outputStream.write(jGcmData.toString()。getBytes());

//读取GCM响应。
InputStream inputStream = conn.getInputStream();
String resp = IOUtils.toString(inputStream);
System.out.println(resp);
System.out.println(请检查您的设备/仿真器是否有通知,或通过logcat获取
+确认收到GCM消息。);
} catch(IOException e){
System.out.println(Unable to send GCM message。);
System.out.println(请确保API_KEY已被服务器
+API密钥替代,并且设备的注册令牌正确(如果指定)。);
e.printStackTrace();
}
}

}



观察结果是一样的,只要示例客户端应用程序在后台,android通知显示为没有任何客户端代码干涉,但是每当示例客户端应用程序处于前台时,android通知都不会显示,但MyGcmListenerService.onMessageReceived字符串来自,捆绑数据)被称为值为:234552842207消息:空。



值得注意的是,这个方法在前面的例子中并没有在示例客户端应用程序在前台调用。



因此,现在有三种可能性。



  • 我们发送带有通知负载的下游消息的方式有些不对或缺失。

    请在这里帮助我。

    解决方案

    所以这可能性不大。 3行为只是这样。



    以下是[email protected]的回复:


    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. If the app is in foreground then no android notification is shown but the onMessageReceived() in GcmListenerService implementation is called with null "message".

    onMessageReceived() is not called when the app is in background and android notification is shown as expected.

    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

    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.

    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.

    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.

    • 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.

    Please help me out here.

    解决方案

    So it was possibility no. 3 "The behavior is like this only."

    Here is the reply from [email protected] :

    这篇关于GCM 3.0通知负载在应用程序处于前景时不显示android通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:33