【问题标题】:The notification service displays the last notification that was set , the ones before don't get displayed通知服务显示最后设置的通知,之前的通知不显示
【发布时间】:2014-08-06 14:18:38
【问题描述】:

首先我知道这个问题被问了很多,唯一的答案是为经理和待处理的Intent设置一个唯一的ID,所以我这样做了:

这是一个代码:

@Override
    public int onStartCommand(Intent i , int flags , int startId){


        try{

            //UnqueID = i.getExtras().getInt(PassedUNIQUEID);

            UniqueID++;

            Log.d("SERVICECHECK" , UniqueID + "");

            mStopIntent = new Intent(this , Service_reminder.class);

            sTimeUntilNotification = 0;

            CalculateTimeInMilSeconds(i);

            final String Title = i.getExtras().getString(PassedSharedPref);
            final String Note = i.getExtras().getString(PassedName);


            //


             Intent notificationIntent = new Intent(Service_reminder.this, activity_todialog_notecontent.class);
             notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_SUBJECTNAME, Title);
             notificationIntent.putExtra(activity_todialog_notecontent.EXTRA_CONTENT, Note);

            PendingIntent contentIntent = PendingIntent.getActivity(Service_reminder.this, UniqueID , notificationIntent,   
                       PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

             builder =  new NotificationCompat.Builder(this).setSmallIcon(R.drawable.iconsubjecttracker)
                        .setContentTitle(Title)
                        .setContentText(Note); 

            builder.setContentIntent(contentIntent);  
            builder.setAutoCancel(true);

            Uri AlarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            builder.setSound(AlarmSound);
            manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  

            Log.d("SERVICECHECK" , sTimeUntilNotification + "");

            CountDownTimer Timer = new CountDownTimer(sTimeUntilNotification , 1000) {

                @Override
                public void onTick(long millisUntilFinished) {

                    //manager.notify(0, builder.build());

                }

                @Override
                public void onFinish() {

                    manager.notify(UniqueID, builder.build());
                    stopService(mStopIntent);

                }
            }.start();

        }catch(NullPointerException e){

            //handle

        }



        return START_STICKY;
    }

它在时间上没有问题,但问题是,例如,当我在笔记上设置提醒“稍后触发通知”时,例如我放了 2 分钟,然后我转到下一个笔记并放了一个提醒一下,让我们说一分钟,然后第一个启动没问题,第二个启动然后更新第一个?所以用户会看到最后一个被触发的通知,我不知道如何解决这个问题。

编辑:

好吧,我迷路了,我一直在尝试解决它几个小时,但没有任何效果,从声明一个静态变量并在每次调用服务时添加 1 到在共享首选项中放置一个唯一 ID 并加载它然后更新它,但没有任何效果。

我将代码编辑为我所做的最新操作,即再次放置一个静态变量并每次都添加一个,因为在我看来这是最干净的解决方案。

希望我明天能找到解决方案。

对于不想阅读所有内容的人来说,这里是一个示例 + 我所做的图片:

先上图:

http://oi62.tinypic.com/2mrs9s0.jpg

如你所见,我调用了两次启动服务,一次是 1 分钟提醒,另一次是 2 分钟提醒,1 和 2 是唯一 id,所以它不可能使用相同的 id。

问题的一个例子:

例如,我有一张写着“数学”的便条,我在 2 分钟后发出了提醒,然后另一个便条上写着“微积分”,我在 1 分钟后发出了提醒,1 分钟后微积分的通知弹出没问题,2分钟后只有声音弹出但没有数学通知,就像我只放了一个微积分笔记一样。

编辑 2:

我在使用警报管理器而不是计数器后解决了这个问题。

【问题讨论】:

  • @SoftCoder 好吧,我尝试设置唯一值,但它不起作用它仍然与我提到的问题相同,这是我在日志猫中获取的唯一值的图片,我输入oi62.tinypic.com/2mrs9s0.jpg,你可以看到第一个1是我第一次调用它时的唯一值,12000是两分钟直到通知弹出,2是我第二次调用服务时的唯一值, 60000 距离弹出通知还有一分钟,但它仍然只显示最后一条通知,但它会弹出 2 分钟通知的声音,可能正在更新
  • 奇怪的是,它只发生在第一个通知和第二个通知之间有时间间隔的情况下,所以如果我尝试输入 0 并快速获得通知,然后将 0 放在另一个注释上收到通知,第一个没有更新或删除,它只是停留在那里没问题。
  • 在您的第二条评论中,您说相同的 id 仍然是通知?
  • @SoftCoder,什么?我尝试过的最后一个解决方案是每次调用 onStartCommand 时都放置一个静态 int 并递增,如图所示,但问题正如我所说,我有一张纸条,上面写着“数学”,例如,我2分钟后放一个提醒,然后另一个说“微积分”的注释,我在1分钟后放了一个提醒,1分钟后微积分的通知弹出没有问题,2分钟后只有声音弹出但没有数学通知,就好像我只写了一个微积分笔记一样。

标签: android


【解决方案1】:

我在服务类中创建的这个方法

这里SIMPLE_NOTFICATION_ID = 1

我也尝试了多个不同的SIMPLE_NOTFICATION_ID,但它不会删除以前的通知。表示效果很好。

   public void getLocation1(){

                timer = new Timer();

                locationManager.

requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 0, this);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, this);

            handler = new Handler();
            locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            mUpdateResults = new Runnable() {
                public void run() {
                    getLatLongFromDatabase();
                    if(isGPSEnabled)
                    locationlist();
                    for(int ii=0;ii<Table_Values_length ; ii++)
                    {

                    if((((Dlat[ii]-0.0002) <= lat) && (lat <= (Dlat[ii]+0.0002))) && (((Dlng[ii]-0.0002) <= lng) && (lng <= (Dlng[ii]+0.0002))) )
                    {
                        String Tooast = name[ii];

                        int idtosent = ids[ii];
                            String a = String.valueOf(lat);
                            String b = String.valueOf(lng);
                            String c = String.valueOf(idtosent);

                        // Toast.makeText(getApplicationContext(),Tooast+"-ID="+idtosent, Toast.LENGTH_SHORT).show();
                         StopContinuousRun = true;
                         Intent notifyIntent = new Intent(ServiceHandler.this, OnNotificationClick.class);
                         notifyIntent.putExtra("data1", a);
                         notifyIntent.putExtra("data2", b);  
                         notifyIntent.putExtra("data3", c);  

                         notifyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                         final PendingIntent Intent = PendingIntent.getActivity(getApplicationContext(), SIMPLE_NOTFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                         Notification notifyDetails  = new Notification.Builder(ServiceHandler.this)
                         .setContentTitle("You Are at "+Tooast)
                         .setContentText("Click to View This Location Tasks")
                         .setSmallIcon(R.drawable.gps_icon)
                         .setContentIntent(Intent)
                         .setAutoCancel(true)
                         .setOnlyAlertOnce(true)
                         .build();

                         notifyDetails.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
                         //  final Notification notifyDetails = new Notification(R.drawable.ic_launcher,
                       //         "You've got a new notification!",System.currentTimeMillis());
                         mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

                         mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
                        ;
                      }
                    else
                    {
                        //  Toast.makeText(getApplicationContext(),"Lat: "+lat +"\n Long: "+lng, Toast.LENGTH_SHORT).show();
                          StopContinuousRun = false;
                    }
                  }
                }
            };
            int delay = 1000 * 5; 
            int period = 8000; 

            timer.scheduleAtFixedRate(new TimerTask() {
            public void run(){handler.post(mUpdateResults); }}, delay, period);

    }

【讨论】:

  • 很抱歉打扰您了,但我查看了通知代码,它与我的相同,只是我使用的是 NotificationCompat.Builder,因为我的目标是 api 级别8 及以上,您的仅适用于 api 级别 11 及以上。
  • 没关系.. 有时间我会尝试再次查看您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 2018-04-11
相关资源
最近更新 更多