2018年12月22日 星期六

Delphi XE10.3 Rio 實作 Android 8 的 Notification Channels

 
{$IFDEF ANDROID}

uses FMX.PushNotification.Android, Androidapi.Helpers, Androidapi.Jni.Os,
  Androidapi.Jni.App, Androidapi.Jni.GraphicsContentViewText,
  Androidapi.Jni.JavaTypes, Androidapi.JNIBridge;
{$ENDIF}

procedure TForm1.createNotificationChannel();
var
  LService: JObject;
  FNotificationManager : JNotificationManager;
  FNotificationChannel: JNotificationChannel;
  LApplicationInfo: JApplicationInfo;
  targetSdkVersion: Integer;
begin
{$IFDEF ANDROID}
  LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.NOTIFICATION_SERVICE);
  FNotificationManager  := TJNotificationManager.Wrap((LService as ILocalObject).GetObjectID);

//    if TAndroidHelperEx.CheckBuildAndTarget(TAndroidHelperEx.OREO) then
// Tony 參考 https://github.com/DelphiWorlds/KastriFree/blob/master/Core/DW.Android.Helpers.pas 改寫
  LApplicationInfo := TAndroidHelper.Context.getPackageManager.
    getApplicationInfo(TAndroidHelper.Context.getPackageName, 0);
  targetSdkVersion := LApplicationInfo.targetSdkVersion;

  if (TJBuild_VERSION.JavaClass.SDK_INT >= 26) and (targetSdkVersion >= 26) then
  begin
    FNotificationChannel := TJNotificationChannel.JavaClass.init(TAndroidHelper.Context.getPackageName, StrToJCharSequence('default'), 4);
    FNotificationChannel.enableLights(True);
    FNotificationChannel.enableVibration(True);
    FNotificationChannel.setLightColor(TJColor.JavaClass.GREEN);
    FNotificationChannel.setLockscreenVisibility(TJNotification.JavaClass.VISIBILITY_PRIVATE);
    FNotificationManager.createNotificationChannel(FNotificationChannel);
  end;
{$ENDIF}
end;

沒有留言:

張貼留言