Android 多个通知不起作用

Android 多个通知不起作用,android,android-intent,android-service,android-notifications,android-alarms,Android,Android Intent,Android Service,Android Notifications,Android Alarms,我在我的应用程序中加入了NotifyService,通过日期选择器在选定的特定日期显示特定通知。。。 问题是最近的通知只会被触发&而不是前一个。。。 我也试着给一个唯一的通知Id,但仍然没有工作 以下是主要活动- public class MainActivity extends Activity { private ScheduleClient scheduleClient; private DatePicker picker; /** Called when the activity i

我在我的应用程序中加入了NotifyService,通过日期选择器在选定的特定日期显示特定通知。。。 问题是最近的通知只会被触发&而不是前一个。。。 我也试着给一个唯一的通知Id,但仍然没有工作

以下是主要活动-

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

附表服务-

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();
}
public class NotifyService extends Service {

/**
 * Class for clients to access
 */
public class ServiceBinder extends Binder {
    NotifyService getService() {
        return NotifyService.this;
    }
}

// Unique id to identify the notification.

public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
private NotificationManager mNM;
private Notification notif;

@Override
public void onCreate() {
    Log.i("NotifyService", "onCreate()");
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

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

    if(intent.getBooleanExtra(INTENT_NOTIFY, false))
        showNotification();

    return START_NOT_STICKY;
}

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

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

/**
 * Creates a notification and shows it in the OS drag-down status bar
 */
private void showNotification() {
    CharSequence title = "Alarm!!";
    int icon = R.drawable.ic_dialog_alert;
    CharSequence text = "Your notification time is upon us.";       

    Notification.Builder notification = new Notification.Builder(NotifyService.this);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
    int Notif = MyApp.preferences.getInt("notif", 1);

    notification.setContentTitle(title)
    .setContentText(text)
    .setSmallIcon(icon)
    .setContentIntent(contentIntent);

    notif = notification.getNotification();
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    mNM.notify(Notif, notif);

    stopSelf();
}}
}

报警任务-

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

&最后,通知服务-

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();
}
public class NotifyService extends Service {

/**
 * Class for clients to access
 */
public class ServiceBinder extends Binder {
    NotifyService getService() {
        return NotifyService.this;
    }
}

// Unique id to identify the notification.

public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
private NotificationManager mNM;
private Notification notif;

@Override
public void onCreate() {
    Log.i("NotifyService", "onCreate()");
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}

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

    if(intent.getBooleanExtra(INTENT_NOTIFY, false))
        showNotification();

    return START_NOT_STICKY;
}

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

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

/**
 * Creates a notification and shows it in the OS drag-down status bar
 */
private void showNotification() {
    CharSequence title = "Alarm!!";
    int icon = R.drawable.ic_dialog_alert;
    CharSequence text = "Your notification time is upon us.";       

    Notification.Builder notification = new Notification.Builder(NotifyService.this);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SecondActivity.class), 0);
    int Notif = MyApp.preferences.getInt("notif", 1);

    notification.setContentTitle(title)
    .setContentText(text)
    .setSmallIcon(icon)
    .setContentIntent(contentIntent);

    notif = notification.getNotification();
    notif.flags |= Notification.FLAG_AUTO_CANCEL;
    mNM.notify(Notif, notif);

    stopSelf();
}}
因此,在这里,当我选择一个日期,例如2017年12月31日,设置Notif&然后再次将另一个设置为2018年1月1日,那么我猜第一个将被删除。。。
任何帮助都将不胜感激:)

在您的
NotifyService
类中,属于
NotificationManager
对象mNM的
notify()
方法需要为通知指定不同的id,否则它将替换现有通知。阅读
NotificationManager.notify()
docs