[英]GCM 3.0 notification payload not showing android notification when app is in foreground
當我發送帶有通知(無數據)有效負載的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種可能性。
請幫幫我。
所以有可能沒有。 3“這種行為只是這樣。”
以下是gcm-dev-support@google.com的回復:
這是為了向您確認,當應用程序在后台時,Android上的通知有效負載將僅顯示在通知托盤上。 如果它位於前台並且您希望部署通知,則可以考慮在示例應用程序中以與此方法類似的方式部署通知。 它是從onMessageReceived()調用的。 希望這可以幫助。
在您的聊天活動中,創建如下所示的廣播接收器,並在onCreate函數中調用它。
添加變量BroadcastReceiver mRegistrationBroadcastReceiver; 到你的班級
private void setUpBroadcastReceiver() {
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//create notification
}
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.