安卓通知错误

标签 android android-notifications proximity

我编写一个 Android 应用程序大约 4 个月了,我目前收到系统通知错误。 问题是我从各种兴趣点添加接近警报,当它接近特定点时,它会发送系统通知。但是现在,当我接近一个以上的点时,它会发送正确数量的通知,但它们都是相同的,具有相同的标题和文本,并且我会传递有关 Intent 的不同信息。 我的代码是:

主要 Activity :

    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;
         }
    }

我希望我解释得很好 :S 提前致谢

最佳答案

您的待定 Intent 正在相互覆盖,因为额外的并不能使它们独一无二。

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

it is important to know when two Intents are considered to be the same for purposes of retrieving a PendingIntent. A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen.

解决这个问题的最好方法是设置请求代码,可能是 id

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

关于安卓通知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21876140/

相关文章:

java - Exo播放器 : setNotificationListener is deprecated

android - 尽管创建了 channel ,但通知未显示在 Android O 中

mysql - 仅获取给定列值的一行

c++ - 检测用户离屏幕的距离

android - Android 中的 float Phonegap Activity

android - 以编程方式在 Google Play 商店上为应用程序启用自动更新

Android Camera2 API 缓冲区和相机断开连接问题

android - 带灰色方 block 的通知背景

search - Solr接近度有序与无序

安卓模拟器占满整个屏幕