GCM 3.0通知负载在应用程序位于前台时不显示android通知
当我发送带有通知(无数据)负载的gcm消息时,只有当我的应用程序在后台时,才会显示android通知。如果应用程序位于前台,则不会显示任何android通知,但GcmListenerService实现中的onMessageReceived()会被调用,并显示null“message” 当应用程序处于后台且android通知按预期显示时,不会调用onMessageReceived() 这是预期的行为还是我遗漏了什么?如果需要任何客户端代码,请让我知道 以下是服务器端代码片段:GCM 3.0通知负载在应用程序位于前台时不显示android通知,android,google-cloud-messaging,Android,Google Cloud Messaging,当我发送带有通知(无数据)负载的gcm消息时,只有当我的应用程序在后台时,才会显示android通知。如果应用程序位于前台,则不会显示任何android通知,但GcmListenerService实现中的onMessageReceived()会被调用,并显示null“message” 当应用程序处于后台且android通知按预期显示时,不会调用onMessageReceived() 这是预期的行为还是我遗漏了什么?如果需要任何客户端代码,请让我知道 以下是服务器端代码片段: Message.Bu
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);
更新
我试着在谷歌提供的示例应用程序b中检查相同的行为,发现它是相同的。那么应该是这样吗
下面是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(字符串来源,捆绑数据)使用以下值调用:234552842207消息:null
需要注意的是,在前面的示例中,当示例客户端应用程序位于前台时,没有调用此方法
所以现在有3种可能性
- 我们发送带有通知有效负载的下游消息的方式存在错误或缺失
- 这种行为只是这样
- 可能是一个错误
请帮帮我 在聊天活动中创建一个如下所示的广播接收器,并在onCreate功能中调用它 添加变量BroadcastReceiver mRegistrationBroadcastReceiver到您的班级
private void setUpBroadcastReceiver() {
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//create notification
}
};
}
因此,第三种可能性是“这种行为只是这样。” 这是gcm开发人员的回复-support@google.com: 这是为了向您确认Android上的通知负载仅在应用程序处于后台时才会显示在通知托盘上。如果它位于前台,并且希望部署通知,则可以考虑在示例应用程序中部署通知。它是从onMessageReceived()调用的。 希望这有帮助
问题是关于GCM3.0的。事情发生了变化。我已经完成了一个聊天应用程序,并使用gcm 3.0交付给客户端。请详细说明你的问题。我们无法想象您的代码会得出正确的结论。gcm消息有两种类型,一种是通知负载,另一种是数据负载。我的问题是关于带有通知负载的消息,它直接显示Android通知,而不需要客户端应用的干预。你用过吗?试试这里的官方回购:在
通知上tab@bjiang我已经更新了这个问题后,通过检查样本应用程序从官方repo这是工作的预期。当应用程序位于前台时,您将在GcmListenerService的onReceived回调中获得消息负载(包括通知)。