我一直在關注Set Up a GCM Client App tutorial並試圖瞭解他們提供的演示應用程序,但我無法理解如何使用此服務發送推送通知。使用Google GCM發送推送通知
以上指南將帶領我進入「生成InstanceID令牌」屏幕,並帶有無限的ProgressBar。這個程序的源碼可以在這裏獲得:https://github.com/googlesamples/google-services/tree/master/android/gcm/app/src/main
使用上面的源代碼後,我嘗試了下簡單的下游消息,這個:https://developers.google.com/cloud-messaging/downstream爲了發送推送給應用程序的用戶。
我無法理解我應該如何向我的應用程序用戶發送通知。
我有一臺服務器,我很擅長使用PHP,爲了使用GCM向我的應用程序用戶發送推送通知,還需做些什麼?
最後設法將消息傳遞給TextView! 如何把它推入?
public class GcmService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
sendNotification("Received: " + jsonObject.toString(5));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onDeletedMessages() {
sendNotification("Deleted messages on server");
}
@Override
public void onMessageSent(String msgId) {
sendNotification("Upstream message sent. Id=" + msgId);
}
@Override
public void onSendError(String msgId, String error) {
sendNotification("Upstream message send error. Id=" + msgId + ", error" + error);
}
private void sendNotification(final String msg) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (MainActivity.mTextView != null) {
MainActivity.mTextView.setText(msg);
}
}
});
}
}
謝謝!我一直在關注它,並最終設法將消息傳遞到我的設備!那很棒。但它顯示了我創建的textview上的消息,並且我需要它將其顯示爲推送通知。即使用戶未使用應用程序,我如何才能將此textview更改爲推送通知? – EliKo
對於系統通知,國際海事組織,你應該[這裏](http://developer.android.com/guide/topics/ui/notifiers/notifications.html),有一個關於「創建一個簡單的通知」的例子 – BNK
你發佈我2個鏈接,我跟着他們,並使GCM消息顯示在一個TextView中,它的工作原理,然後你引薦我一個不同的類,我不知道如何實現在給定的代碼後面的前2鏈接。如何在上面編輯的sendNotification方法中實現此通知? – EliKo