通知未出现通知托盘

Notification not appearing Notification Tray

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

问题是,当我检查某个位置且响应成功时,一切都很顺利,甚至 toast 显示它符合条件的位置,但我没有收到任何通知。

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

数据源:

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

使用它来生成通知

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

你错过了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;
                }