Einzelnen Beitrag anzeigen

bcvs

Registriert seit: 16. Jun 2011
680 Beiträge
 
Delphi 12 Athens
 
#17

AW: Notifications mit individuellem Sound

  Alt 13. Mai 2024, 16:37
Sorry, ich hatte vergessen, meine Lösung hier zu posten.

Man muss das über einen Channel machen. Ich hatte dazu ein Ticket im Customer Support Portal eröffnet. Die Antwort:

Zitat:
I must stay there isn't a lot of information on this either in the Android documentation or online so here are the important points.

1) sound needs to be from presented as "content://" it be to make is shareable with the external OS to play the notification sound., because the notification is set up using an Intent and that means the it has to be a shareable URI.

2) the sound can only be set at the channel level AND when the channel is created, so the default channel will always have the default sound. So to have new sound you have to set it at when a new channel is created.
3) Setting the sound at the notification level has been deprecated in Android 7 or 8, that sound is not used at all. (I learnt this from the looking at the Android source)
4) I have modified your code so I could figure out how the notifications work, but it all boils down to a simple function to get the file URI, and to use the file URI you need to need to specify secure file sharing in the entitlements list, in the project options, this will give you the ability to use the file provider.
So läuft's bei mir:
Delphi-Quellcode:
{$ifdef ANDROID}

const
    sChannelID = 'MyChannelID'; // to have a custom sound you HAVE to use custom channel
                                // as the channel's sound is set at creation

//this is crucial bit...
// to use fileprovider you need to specify secure file sharing in the entitlements list
function GetFileUriStr(const AFileName: string): string;
var
  LFile: JFile;
begin
  LFile := TJFile.JavaClass.init(StringToJString(AFileName));
  Result := JStringToString(TAndroidHelper.JFileToJURI(LFile).toString);
end;

{$endif}

procedure TdmNotification.CreateDefaultNotificationChannel;
// wird vorab aufgerufen, z.B. im Create
var
 NotificationChannel: TChannel;
begin
 NotificationChannel := NotificationCenter.CreateChannel;
 NotificationChannel.Id := sChannelID;
 NotificationChannel.Title := 'Custom notification channel';
 NotificationChannel.Description := 'Notification channel to test sound';
 NotificationChannel.Importance := TImportance.High; // NOTE: This is a 'heads-up notification'.
 //the CONTENT URI string needs to be set here and will be used for the all THIS channel notifications
 NotificationChannel.SoundName:= GetFileUriStr(GetSoundFileName);
 NotificationCenter.CreateOrUpdateChannel(NotificationChannel);
end;


function TdmNotification.GetSoundFileName: string;
begin
{$IFDEF IOS}
  Result := 'mixkit-bell-notification-933.caf';
{$ELSE}
  Result := IncludeTrailingPathDelimiter(TPath.GetDocumentsPath) + 'mixkit-bell-notification-933.mp3';
{$ENDIF}
end;

procedure TdmNotification.CreateNotification(AlarmTime);
var Notification: TNotification;
begin
  Notification := NotificationCenter.CreateNotification;
  try
    Notification.Name :='MyNotification';
    Notification.Title :='Test Notification Title';
    Notification.AlertBody:='Test Notification with individual sound';
    {$ifdef ANDROID}
    Notification.ChannelId := sChannelID;
    {$else}
    Notification.EnableSound:=true;
    Notification.SoundName:= GetSoundFileName;
    {$endif}

    Notification.FireDate:= AlarmTime;

    NotificationCenter.ScheduleNotification(Notification);
  finally
    Notification.Free;
  end;
end;
  Mit Zitat antworten Zitat