2015-10-07 44 views
1

我想檢查應用程序通知是否正在運行。我曾嘗試過各種illustration。即使通知正在運行,測試也總是返回錯誤。實施通知爲:檢查是應用程序通知正在運行

NotificationManager mNotificationManager = (NotificationManager) 
        context.getSystemService(Context.NOTIFICATION_SERVICE); 
      RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.zone_recharge_notification); 
      remoteViews.setOnClickPendingIntent(R.id.zn_recharge, getPendingSelfIntent(context)); 
      remoteViews.setOnClickPendingIntent(R.id.zn_launch, getLaunchZone(context)); 

      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(context) 
          .setSmallIcon(R.drawable.zone_holo_logo_48) 
          .setOngoing(true) 
          .setOnlyAlertOnce(true) 
          .setPriority(NotificationCompat.PRIORITY_MAX) 
          .setContent(remoteViews) 
          .setAutoCancel(false); 


      mNotificationManager.notify(AppConstant.RECHARGE_ME_NOTIFICATION_ID, mBuilder.build()); 

並使用下面的代碼段檢查它是否正在運行。

public static boolean isNotificationVisible(Context context) { 
     Intent notificationIntent = new Intent(context, WindowServiceDialog.class); 
     PendingIntent test = PendingIntent.getService(context, AppConstant.RECHARGE_ME_NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE); 
     return test != null; 
    } 

this但被在API級別18和我想要的東西,可以在集成電路設備上工作。我怎樣才能解決這個問題 ?謝謝。

編輯

未決的每個遠程視窗組件

protected PendingIntent getPendingSelfIntent(Context context) { 
     Intent intent = new Intent(context, AppReceiver.class); 
     return PendingIntent.getBroadcast(context, 0, intent, 0); 
    } 

    protected PendingIntent getLaunchZone(Context context) { 
     Intent intent = new Intent(context, WindowServiceDialog.class); 
     return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    } 
+0

'isNotificationVisible()'應該如何工作?你只是創建一個新的PendingIntent實例。 'getService'但是並不意味着它已經存在'PendingIntent',它每次都會創建一個新的實例。 –

+0

並回答您的問題:最好的方法是以不需要知道哪些通知可見的方式設計您的應用。最好只顯示通知,如果需要的話,然後忘記它。後來需要引用通知的某種邏輯是一件壞事。但是,如果您確實需要知道通知是否可見和/或通知是什麼,那麼您始終可以將其自己保存在某個地方,例如在數據庫中。 –

+0

從問題上的stackoverflow線程引用,isNotificationVisible()應該返回true是一個通知正在運行與指定的掛起意圖 –

回答

-1

隨着mBuilder.setDeleteIntent(deletePendingIntent);javadoc),您可以設定觸發您的通知掛起的意圖,在通知是cleard的意圖。然後創建一個IntentService(或任何其他的Intent接收器),記錄刪除並將該信息存儲在某處(例如SharedPreferences)。在您的isNotificationVisible操作中,您可以檢查sharedPref中的值。

+0

爲什麼和誰投票呢?我們在生產中使用它來收集當前顯示的通知中的信息... –

相關問題