通知没有出现通知托盘

时间:2015-06-08 12:25:30

标签: android notifications

在我的应用程序中,我有一个LocationTester Service implements LocationListener,负责从sqlite Db检索提醒,并用onLocationChanged()方法将它们的距离与当前位置进行比较,然后在距离小于1km时显示信息然后抛出与该特定提醒相关的通知。

问题在于,当我检查某个位置并且响应成功时,即使吐司正在显示满足条件但我没有得到任何通知的情况下,每个事情都顺利进行。

LocationTester服务:

public void onLocationChanged(Location location) {
        dataSrc.open();
        ArrayList<Reminders> rems = (ArrayList<Reminders>) dataSrc.findALL();

        for(Reminders x:rems){
            Location locs = new Location("");
            locs.setLatitude(x.getLat());
            locs.setLongitude(x.getLng());

            double dis = location.distanceTo(locs);
            if(dis<=1000){
                String title = x.getTitle();
                String des = x.getDescription();
                Intent intent = new Intent(this,ActivityShowReminders.class);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                builder.setContentTitle(title);
                builder.setContentText(des);
                noteManag = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                noteManag.notify(noteId,builder.build());
                noteId += 1;
                Toast.makeText(this, title+"("+des+")", Toast.LENGTH_LONG).show();
            }
        }

DATASRC:

public List<Reminders> findALL(){
        List<Reminders> reminder = new ArrayList<Reminders>();
        Cursor cursor = database.query(ReminderDBOpenHelper.TABLE_REMINDER, allColumns, 
                null, null, null, null, null);
        Log.i(LOG_TAG,cursor.getCount()+" rows");
        if(cursor.getCount()>0){
            while(cursor.moveToNext()){
                Reminders reminders = new Reminders();
                reminders.setId(cursor.getLong(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_ID)));
                reminders.setTitle(cursor.getString(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_TITLE)));
                reminders.setDescription(cursor.getString(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_DESCRIPTION)));
                reminders.setLat(cursor.getDouble(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_LAT)));
                reminders.setLng(cursor.getDouble(cursor.getColumnIndex(ReminderDBOpenHelper.COLUMN_LONG)));
                reminder.add(reminders);
            }
        }

2 个答案:

答案 0 :(得分:0)

用于生成通知

    static int uniqueID = 0 ;
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(context);
    builder.setContentTitle(title);
    builder.setContentText("text");
    builder.setTicker("Ticker");
    builder.setSmallIcon(R.drawable.icon);
    builder.setVibrate(new long[]{100, 1500});
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    uniqueID = uniqueID + 1;
    notificationManager.notify(uniqueID, notification);

答案 1 :(得分:0)

您遗失builder.setSmallIcon(R.drawable.remindericon);没有此通知将不会显示。 并且illegalargumentexception contentintent required notifiction尝试附加未决意图。

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                    Intent notificationIntent = new Intent(this,YourTargetClass.class );
                    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
                    notification.contentIntent = contentIntent;
                }