2014-02-19 57 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