只是为了补充 René Hoffmann 的回答
你只能使用那些重复间隔的原因是因为你不能用 iOS 设置自定义重复间隔,
因为它使用了它的UILocalNotification 对象,并且它的RepeatInterval 需要一个NSCalendarUnit,它是一个枚举类型。
https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/repeatInterval
但是在另一方面,如果你想发出重复通知,你可以使用AlarmManager.setRepeating 方法
因此,如果您想制作适用于 Android 的本机重复通知,您可以这样做:
TNotification 驻留在System.Notification
给它添加一个属性:
{$IFDEF ANDROID}
RepeatIntervalinMills : Integer;
{$ENDIF}
在TNotification.Create 中只需给它一个默认值
{$IFDEF ANDROID}
RepeatIntervalinMills := 0;
{$ENDIF}
现在我们需要添加原生 Android 方法来设置重复通知,为此您需要导航到 System.Android.Notification
找到TNotificationCenterAndroid.DoScheduleNotification,现在只需要添加一些代码,这样如果您没有指定RepeatIntervalinMills,则只会创建标准通知:
begin
if not ANotification.Name.IsEmpty and FExternalStore.Contains(ANotification.Name) then
CancelNotification(ANotification.Name);
ID := TGeneratorUniqueID.GenerateID;
PendingIntent := CreateNotificationAlarmIntent(ID);
FExternalStore.SaveNotification(ANotification, ID);
if ANotification.RepeatIntervalinMills <> 0 then
begin
TAndroidHelper.AlarmManager.setRepeating(TJAlarmManager.JavaClass.RTC_WAKEUP, DateTimeLocalToUnixMSecGMT(ANotification.FireDate),
ANotification.RepeatIntervalinMills,PendingIntent);
end
else
TAndroidHelper.AlarmManager.&set(TJAlarmManager.JavaClass.RTC_WAKEUP, DateTimeLocalToUnixMSecGMT(ANotification.FireDate),
PendingIntent);
end;
现在当您创建通知时:
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'MyNotification';
MyNotification.AlertBody := 'Hello!';
{$IFDEF IOS}
//Repeat very 5 minutes
//Create 12 notifications fireing every hour with 5 minute intervals Notification.RepeatInterval := TRepeatInterval.Hour;
for I := 0 to 11 do
begin
Notification.FireDate := Notification.FireDate + EncodeTime(0,(I*5),0,0);
ANotificationcenter.ScheduleNotification(Notification);
end;
{$ENDIF}
{$IFDEF ANDROID}
MyNotification.FireDate := IncMinute(Now,5);
MyNotification.RepeatIntervalinMills := 300000; //Now you can specify your Custom Android Repeat interval
NotificationCenter1.ScheduleNotification(MyNotification);
{$ENDIF}
finally
MyNotification.DisposeOf;
end;
这将创建一个在 5 分钟内触发并每 5 分钟重复一次的通知