【发布时间】:2015-12-01 10:28:01
【问题描述】:
当我发送带有通知(无数据)有效负载的 gcm 消息时,只有当我的应用程序在后台时才会显示 android 通知。如果应用程序在前台,则不会显示 android 通知,但 GcmListenerService 实现中的 onMessageReceived() 会以 null“消息”调用。
onMessageReceived() 不会在应用程序处于后台并且按预期显示 android 通知时调用。
这是预期的行为还是我错过了什么?如果需要任何客户端代码,请告诉我。
这里是服务器端代码sn-p:
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 示例服务器端代码,我们在其中进行了细微更改以发送仅包含通知有效负载的消息。
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 Message: null。
需要注意的是,在之前的例子中,当示例客户端应用程序在前台时,这个方法没有被调用。
所以现在有 3 种可能性。
- 我们发送带有通知负载的下游消息的方式有问题或缺失。
- 行为只有这样。
- 可能存在错误。
请帮帮我。
【问题讨论】:
-
在这里尝试官方回购:github.com/google/gcm/tree/master/samples/android/gcm-demo 在
notification标签上 -
@bjiang 我在检查了官方仓库中的示例应用程序后更新了问题
-
这是按预期工作的。当应用程序处于前台时,您将在 GcmListenerService 的 onReceived 回调中获取消息负载(包括通知)。
标签: android google-cloud-messaging