GCM 推送消息以在前台更新 UI 并在后台更新 post 通知。如何实施?
GCM Push message to update UI when foreground and post notification when background. How to implement this?
我收到了 GCM 推送消息。
如何决定我是否必须更新 UI 或 post 通知。
如果我们的应用程序在前台,那么我们可以更新 UI。如果我们的应用不是 运行,那么我需要 post 通知。这是否是正确的方法。或者任何其他方式来处理它。以及如何找到我的应用程序在前台或后台。
提前致谢。
如果您遵循 Android Dev(具体来说,this section)的指南,您的应用应该会发布通知,无论它是否在前台。
但是,如果你想改变前台Activity
的UI,修改sendNotification()
方法里面的PendingIntent
来启动你的Activity
。您可以在关联的 Intent
中附加额外内容。如果您的 Activity
在后台,它将启动,并且可以通过 Activity
的生命周期方法中的 getIntent()
获得附加功能。如果它在前台,将调用 Activity
的 onNewIntent()
方法,您可以从那里再次获取额外内容(您从通知中发送的内容)。
是的,我自己找到了答案。
There are 3 different your current activity status.
- Resumed - Static reference is available
- Stopped - Static reference is available
- Destroyed - Static references are garbaged
//Define static variable of your activity instance
public static FleetLocActivity mFleetLocActivity = null;
并在您的 GCMIntentService 中编写以下代码。只要有新的推送消息,就会调用此 class。
if (FleetLocActivity.mFleetLocActivity != null) {
**//Activity is in Stopped or Resumed State**
handlePushMessage(pushMessage);
// Start Service and Update UI with the help of Handler
} else {
**//Activity is in Destroyed State**
// Post notification of received message. And Add the action of opening your home activity. When the user clicks the notification it will open the home activity and start the respective service by using activity instance.
mAppUtilInstance.postGcmCommandNotification(
"Command Received : " + pushMessage, mContext);
}
//Class variable updated with the received push message
private String pushMessage = "";
private void handlePushMessage(String pushMsg){
Message msgObj = gcmCommandHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(Constants.getInstance().GCM_SERVER_MESSAGE,pushMessage);
msgObj.setData(bundle);
gcmCommandHandler.sendMessage(msgObj);
}
/**
* to handle the GCM push message (or) commands using Handler
*/
private Handler gcmCommandHandler = new Handler() {
// Create handleMessage function
public void handleMessage(Message message) {
String pushMessage = message.getData().getString(
Constants.getInstance().GCM_SERVER_MESSAGE);
if (pushMessage != null && pushMessage.length() > 0) {
//Start Service and update UI HERE
}
}
};
我收到了 GCM 推送消息。
如何决定我是否必须更新 UI 或 post 通知。
如果我们的应用程序在前台,那么我们可以更新 UI。如果我们的应用不是 运行,那么我需要 post 通知。这是否是正确的方法。或者任何其他方式来处理它。以及如何找到我的应用程序在前台或后台。
提前致谢。
如果您遵循 Android Dev(具体来说,this section)的指南,您的应用应该会发布通知,无论它是否在前台。
但是,如果你想改变前台Activity
的UI,修改sendNotification()
方法里面的PendingIntent
来启动你的Activity
。您可以在关联的 Intent
中附加额外内容。如果您的 Activity
在后台,它将启动,并且可以通过 Activity
的生命周期方法中的 getIntent()
获得附加功能。如果它在前台,将调用 Activity
的 onNewIntent()
方法,您可以从那里再次获取额外内容(您从通知中发送的内容)。
是的,我自己找到了答案。
There are 3 different your current activity status.
- Resumed - Static reference is available
- Stopped - Static reference is available
- Destroyed - Static references are garbaged
//Define static variable of your activity instance
public static FleetLocActivity mFleetLocActivity = null;
并在您的 GCMIntentService 中编写以下代码。只要有新的推送消息,就会调用此 class。
if (FleetLocActivity.mFleetLocActivity != null) {
**//Activity is in Stopped or Resumed State**
handlePushMessage(pushMessage);
// Start Service and Update UI with the help of Handler
} else {
**//Activity is in Destroyed State**
// Post notification of received message. And Add the action of opening your home activity. When the user clicks the notification it will open the home activity and start the respective service by using activity instance.
mAppUtilInstance.postGcmCommandNotification(
"Command Received : " + pushMessage, mContext);
}
//Class variable updated with the received push message
private String pushMessage = "";
private void handlePushMessage(String pushMsg){
Message msgObj = gcmCommandHandler.obtainMessage();
Bundle bundle = new Bundle();
bundle.putString(Constants.getInstance().GCM_SERVER_MESSAGE,pushMessage);
msgObj.setData(bundle);
gcmCommandHandler.sendMessage(msgObj);
}
/**
* to handle the GCM push message (or) commands using Handler
*/
private Handler gcmCommandHandler = new Handler() {
// Create handleMessage function
public void handleMessage(Message message) {
String pushMessage = message.getData().getString(
Constants.getInstance().GCM_SERVER_MESSAGE);
if (pushMessage != null && pushMessage.length() > 0) {
//Start Service and update UI HERE
}
}
};