如何在Delphi中显示多行GCM推送通知?

时间:2017-04-19 17:50:04

标签: android ios delphi notifications push

使用Berlin / Tokyo和Kinvey,当收到包含长消息文本的GCM推送通知时,只显示一行,剩下的文本被删除。

挖掘互联网,似乎要以全长显示这些通知,需要设置BigContentView,但Delphi不会公开它。

有谁知道如何处理这个,所以通知会全长显示?

1 个答案:

答案 0 :(得分:0)

您有两个选择:

破解单位System.Android.Notification.pas / System.Notification.pas以添加您需要的功能。这很容易,只需要更新一个功能,这个:

function TNotificationCenterAndroid.CreateNativeNotification(const ANotification: TNotification): JNotification;

  function GetDefaultNotificationSound: Jnet_Uri;
  begin
    Result := TJRingtoneManager.JavaClass.getDefaultUri(TJRingtoneManager.JavaClass.TYPE_NOTIFICATION);
  end;

  function GetDefaultIconID: Integer;
  begin
    Result := TAndroidHelper.Context.getApplicationInfo.icon;
  end;

  function GetDefaultIcon: JBitmap;
  begin
    Result := TJBitmapFactory.JavaClass.decodeResource(TAndroidHelper.Context.getResources(), GetDefaultIconID);
  end;

  function GetContentTitle: JCharSequence;
  begin
    if ANotification.Title.IsEmpty then
      Result := StrToJCharSequence(TAndroidHelper.ApplicationTitle)
    else
      Result := StrToJCharSequence(ANotification.Title);
  end;

  function GetContentText: JCharSequence;
  begin
    Result := StrToJCharSequence(ANotification.AlertBody);
  end;

  function GetContentIntent: JPendingIntent;
  var
    Intent: JIntent;
  begin
    Intent := TAndroidHelper.Context.getPackageManager().getLaunchIntentForPackage(TAndroidHelper.Context.getPackageName());
    Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_SINGLE_TOP or TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP);
    SaveNotificationIntoIntent(Intent, ANotification);
    Result := TJPendingIntent.JavaClass.getActivity(TAndroidHelper.Context, TGeneratorUniqueID.GenerateID, Intent, TJPendingIntent.JavaClass.FLAG_UPDATE_CURRENT);
  end;

var
  NotificationBuilder: JNotificationCompat_Builder;
begin
  NotificationBuilder := TJNotificationCompat_Builder.JavaClass.init(TAndroidHelper.Context);
  NotificationBuilder := NotificationBuilder.setDefaults(TJNotification.JavaClass.DEFAULT_LIGHTS);
  if ANotification.SmallIconId <> 0 then NotificationBuilder := NotificationBuilder.setSmallIcon(ANotification.SmallIconId)
  else NotificationBuilder := NotificationBuilder.setSmallIcon(GetDefaultIconID);
  if ANotification.largeIconObj <> nil then NotificationBuilder := NotificationBuilder.setLargeIcon(ANotification.largeIconObj);
  NotificationBuilder := NotificationBuilder.setContentTitle(GetContentTitle);
  NotificationBuilder := NotificationBuilder.setContentText(GetContentText);
  NotificationBuilder := NotificationBuilder.setTicker(GetContentText);
  NotificationBuilder := NotificationBuilder.setContentIntent(GetContentIntent);
  NotificationBuilder := NotificationBuilder.setNumber(ANotification.Number);
  NotificationBuilder := NotificationBuilder.setAutoCancel(True);
  NotificationBuilder := NotificationBuilder.setWhen(TJDate.Create.getTime);
  if (ANotification.Color <> TalphaColorRec.null) and
     (TJBuild_VERSION.JavaClass.SDK_INT >= 21) then NotificationBuilder := NotificationBuilder.setColor(ANotification.Color);
  if ANotification.VibratePattern <> nil then NotificationBuilder := NotificationBuilder.setVibrate(ANotification.VibratePattern);

  if ANotification.EnableSound then
    if ANotification.SoundName.IsEmpty then
      NotificationBuilder := NotificationBuilder.setSound(GetDefaultNotificationSound)
    else
      NotificationBuilder := NotificationBuilder.setSound(StrToJURI(ANotification.SoundName));

  // Action buttons won't appear on platforms prior to Android 4.1!!!
  // http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#addAction
  Result := NotificationBuilder.Build;
end;

或者第二个变体,避免使用delphi TnotificationCenter并从头开始构建自己的