多个通知无效

时间:2017-12-31 07:23:31

标签: android android-intent android-service android-notifications android-alarms

我在我的应用程序中包含了一个NotifyService,通过日期选择器显示所选特定日期的特定通知... 问题是最新通知只是被解雇了。不是前一个...... 我也试过给出一个唯一的通知ID,但它仍然没有用......

继承MainActivity -

v-model

}

ScheduleClient -

public class MainActivity extends Activity  {
private ScheduleClient scheduleClient;
private DatePicker picker;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scheduleClient = new ScheduleClient(this);
    scheduleClient.doBindService();

    picker = (DatePicker) findViewById(R.id.scheduleTimePicker);
}


public void onDateSelectedButtonClick(View v){
    int id = MyApp.preferences.getInt("notif", 0);
    id++;
    MyApp.preferences.edit().putInt( "notif" , id).apply();

    int day = picker.getDayOfMonth();
    int month = picker.getMonth();
    int year = picker.getYear();

    Calendar c = Calendar.getInstance();
    c.set(year, month, day);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
    scheduleClient.setAlarmForNotification(c);

    Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();
}

@Override
protected void onStop() {
    if(scheduleClient != null)
        scheduleClient.doUnbindService();
    super.onStop();
}

}

安排服务 -

public class ScheduleClient {

private ScheduleService mBoundService;
private Context mContext;
private boolean mIsBound;

public ScheduleClient(Context context) {
    mContext = context;
}


public void doBindService() {
    mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}


private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};


public void setAlarmForNotification(Calendar c){
    mBoundService.setAlarm(c);
}


public void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        mContext.unbindService(mConnection);
        mIsBound = false;
    }
}

}

AlarmTask -

public class ScheduleService extends Service{

public class ServiceBinder extends Binder {
    ScheduleService getService() {
        return ScheduleService.this;
    }
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("ScheduleService", "Received start id " + startId + ": " + intent);

    // We want this service to continue running until it is explicitly stopped, so return sticky.
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

// This is the object that receives interactions from clients. See
private final IBinder mBinder = new ServiceBinder();


public void setAlarm(Calendar c) {
    new AlarmTask(this, c).run();
}

}

&安培;最后是NotifyService -

public class AlarmTask implements Runnable{

private final Calendar date;
private final AlarmManager am;
private final Context context;

public AlarmTask(Context context, Calendar date) {
    this.context = context;
    this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    this.date = date;
}

@Override
public void run() {
    Intent intent = new Intent(context, NotifyService.class);
    intent.putExtra(NotifyService.INTENT_NOTIFY, true);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

    // Sets an alarm - note this alarm will be lost if the phone is turned off and on again
    am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}

所以在这里,当我选择一天例如2017年3月31日,设置Notif&然后再设置另一个到1/1/2018然后第一个被删除我猜... 任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:1)

NotifyService课程中,属于notify()对象mNM的NotificationManager方法需要使用不同的ID作为通知,否则它将替换现有通知。阅读NotificationManager.notify() docs here