2014-02-19 59 views
1

我現在編碼一個android應用程序約4個月,im當前正在系統通知上出現錯誤。 問題是,即時通訊從多個興趣點添加鄰近警報,當它接近特定點時,它會發送系統通知。但是現在,當我接近一個以上的點時,它會發送正確數量的通知,但它們都是相同的,具有相同的標題和文本,並且即時通過關於意圖的不同信息。 我的代碼是:Android通知錯誤

MainActivity:

private void addProximityAlert(double latitude, double longitude, String type, int id, String object) { 
        Intent intent = new Intent(PROX_ALERT_INTENT); 
        intent.putExtra("type", type); 
        intent.putExtra("id", id); 
        intent.putExtra("object", object); 
        PendingIntent proximityIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 
        locationManager.addProximityAlert(
          latitude, // the latitude of the central point of the alert region 
          longitude, // the longitude of the central point of the alert region 
          POINT_RADIUS, // the radius of the central point of the alert region, in meters 
          PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no       expiration 
          proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected 
        ); 

        IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
        registerReceiver(new ProximityReceiver(), filter); 
      } 

感應接收器:

public class ProximityReceiver extends BroadcastReceiver{ 


     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 
      String key = LocationManager.KEY_PROXIMITY_ENTERING; 
      Boolean entering = intent.getBooleanExtra(key, false); 
      int id = intent.getIntExtra("id", 0); 
      String type = intent.getStringExtra("type"); 
      String name = intent.getStringExtra("object"); 
      Log.d("NOTIFICATION", "NOME: " + name); 
      Log.d("NOTIFICATION", "ID: " + id); 
      Log.d("NOTIFICATION", "TYPE:" + type); 


      if (entering) { 
         Log.d(getClass().getSimpleName(), "entering"); 
       }else { 
         Log.d(getClass().getSimpleName(), "exiting"); 
       } 
       NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

       Intent notificationIntent = new Intent(context, InterestPoint_Description.class); 
       notificationIntent.putExtra("id", id); 
       notificationIntent.putExtra("type", type); 
       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
       Notification notification = createNotification(); 
       notification.setLatestEventInfo(context, "LisbonRoutes", "You are near " + name + ", touch here to check it out!", pendingIntent); 

       notificationManager.notify(Config.NOTIFICATION_ID, notification); 
       Config.NOTIFICATION_ID++; 

}

 private Notification createNotification() { 
       Notification notification = new Notification(); 
       notification.icon = R.drawable.icon; 
       notification.when = System.currentTimeMillis(); 
       notification.flags |= Notification.FLAG_AUTO_CANCEL; 
       notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
       notification.defaults |= Notification.DEFAULT_VIBRATE; 
       notification.defaults |= Notification.DEFAULT_LIGHTS; 
       notification.ledARGB = Color.WHITE; 
       notification.ledOnMS = 1500; 
       notification.ledOffMS = 1500; 
       return notification; 
     } 
    } 

我希望我已經解釋得很好:提前小號 感謝

+0

「發送通知的正確的量,但他們都是。」他們都是什麼? –

+0

對不起,他們都是一樣的,具有相同的文本,標題等:s – ralbuquerque

回答

2

您的未決意圖將互相覆蓋,因爲附加內容不會使它們獨一無二。

http://developer.android.com/reference/android/app/PendingIntent.html

才知道當兩個意圖被認爲是用於檢索的PendingIntent的目的同樣是非常重要的。人們犯的一個常見錯誤是創建多個PenttentIntent對象,其Intents只在其「額外」內容中有所不同,期望每次都得到不同的PendingIntent。這不會發生。

解決這個

最好的辦法是設置請求代碼,可能是ID

PendingIntent proximityIntent = PendingIntent.getBroadcast(context, id, intent, 0);

+0

那麼我怎麼能讓他們獨特? :S – ralbuquerque

+0

看到編輯你將需要設置不同的requestCode爲每個 –

+0

非常感謝:D – ralbuquerque