我正在使用以下链接的教程来设置提醒。我成功设置了一个提醒,但对于多个提醒,它仍然需要一些调整。
http://blog.blundellapps.co.uk/notification-for-a-user-chosen-time/
我已将notification_id
和notificationText
传递给函数setAlarmForNotification
以及每个新的提醒请求(以唯一标识它们),但似乎它们都被替换为最后的提醒。有趣的是,在logcat中打印的notification_id
和notificationText
是第一个提醒。
NotifyService.java
public class NotifyService extends Service {
//Text string of the notification
private String notificationText;
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
//private static final int NOTIFICATION = 123;
// Name of an intent extra we can use to identify if this service was started to create a notification
public static final String INTENT_NOTIFY = "com.blundell.tut.service.INTENT_NOTIFY";
// The system notification manager
private NotificationManager mNM;
@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);
notificationText = intent.getStringExtra("notification_text");
int notiId = Integer.parseInt(intent.getStringExtra("notification_id"));
// If this service was started by out AlarmTask intent then we want to show our notification
if(intent.getBooleanExtra(INTENT_NOTIFY, false))
showNotification(notiId);
// We don't care if this service is stopped as we have already delivered our notification
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(int notiId) {
// This is the 'title' of the notification
CharSequence title = "Alarm!!";
// This is the icon to use on the notification
int icon = R.drawable.ic_card_giftcard_white_24dp;
// This is the scrolling text of the notification
CharSequence text = "Your notification time is upon us.";
// What time to show on the notification
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
//notification.setLatestEventInfo(this, title, text, contentIntent);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_shopping_cart_white_24dp)
.setContentTitle(title)
.setContentText(notificationText);
notification = builder.build();
// Clear the notification when it is pressed
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify(notiId, notification);
// Stop the service when we are finished
stopSelf();
}
}
应该更改哪些内容才能使其正常工作?
答案 0 :(得分:0)
我相信您的所有闹钟都在运行,但如果您检查\z
它有字段
NotifyService,java
这意味着当您触发每个单独的警报时,他们将启动该服务,但他们生成的通知将始终具有相同的ID。
发布要在状态栏中显示的通知。 如果您的应用程序已经发布了具有相同ID的通知但尚未取消,则该通知将被更新的信息替换。
要解决此问题,您可能希望将该ID设为常量。也许在额外的意图中传递ID,您可以使用 private static final int NOTIFICATION = 123;
内部intent.putExtra(NotifyService.INTENT_NOTIFY, true);
的相同方式来暗示如何执行此操作。