自定义通知间隔
Custom notification interval
我正在 Rad Studio XE8 下的 FireMonkey 中构建一个应用程序。
我想在我的通知上设置自定义间隔 (FMX.Notification)。
但是通知重复只能设置为特定的时间间隔。
TRepeatInterval = (None, Second, Minute, Hour, Day, Week, Weekday, Month, Quarter, Year, Era);
如果我想每 15 分钟触发一次,我真的需要创建四个通知(在 0、15、30、45 分钟)并使用 TRepeatInterval(4) 每小时重复一次吗?
FMX.Notification.TNotification.RepeatInterval
的 documentation 强调说:
If you want to set a custom interval, like for example 30 minutes, you need to create two notifications setting a scheduled difference of 30 minutes with FireDate and set the repeat interval of both notifications to an hour.
你猜对了。您需要创建四个通知并每小时重复一次。
OP 在评论中说,他最终使用了以下代码。我将其包含在我的回答中以提高他给定信息的可读性。
//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;
只是添加到 René Hoffmann 的回答中
您只能使用那些重复间隔的原因是因为您无法使用 iOS 设置自定义重复间隔,
因为它使用它的 UILocalNotification
对象,它的 RepeatInterval 需要一个 NSCalendarUnit
,它是一个枚举类型。
但是 Android 另一方面,如果你想发出重复通知,你可以使用 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 分钟重复一次的通知
我正在 Rad Studio XE8 下的 FireMonkey 中构建一个应用程序。
我想在我的通知上设置自定义间隔 (FMX.Notification)。
但是通知重复只能设置为特定的时间间隔。
TRepeatInterval = (None, Second, Minute, Hour, Day, Week, Weekday, Month, Quarter, Year, Era);
如果我想每 15 分钟触发一次,我真的需要创建四个通知(在 0、15、30、45 分钟)并使用 TRepeatInterval(4) 每小时重复一次吗?
FMX.Notification.TNotification.RepeatInterval
的 documentation 强调说:
If you want to set a custom interval, like for example 30 minutes, you need to create two notifications setting a scheduled difference of 30 minutes with FireDate and set the repeat interval of both notifications to an hour.
你猜对了。您需要创建四个通知并每小时重复一次。
OP 在评论中说,他最终使用了以下代码。我将其包含在我的回答中以提高他给定信息的可读性。
//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;
只是添加到 René Hoffmann 的回答中
您只能使用那些重复间隔的原因是因为您无法使用 iOS 设置自定义重复间隔,
因为它使用它的 UILocalNotification
对象,它的 RepeatInterval 需要一个 NSCalendarUnit
,它是一个枚举类型。
但是 Android 另一方面,如果你想发出重复通知,你可以使用 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 分钟重复一次的通知