![](/img/trans.png)
[英]In Android how can i know the current notification id to clear the notification
[英]How can I know the application state of an Android App to display or not a notification?
我正在開發一個使用Google Cloud Messaging的即時消息應用程序。 當用戶收到聯系人的消息時,我會發送通知 。 一切正常。 我設置了GCMBroadcastReceiver和GCMMessageHandler 。 問題是:即使用戶當前正在與他的聯系人聊天,通知也始終顯示。 我如何知道GCMMessageHandler中的應用程序狀態以不顯示通知?
這是GCMBroadcastReceiver:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
這是GCMMessageHandler:
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
mes = extras.getString("message");
if(IM_NOT_IN_THE_CHAT_ACTIVITY)
{
showNotification(mes);
}
我正在努力獲得的是該標志: IM_NOT_IN_THE_CHAT_ACTIVITY。
如何在IntentService中知道這一點?
這樣做可以達到我的目的:
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
String activity_name = taskInfo.get(0).topActivity.getClassName();
if(activity_name != ACTIVITY_NAME)
showNotification(mes);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.