【问题标题】:How to show multi-line GCM push notifications in Delphi?如何在 Delphi 中显示多行 GCM 推送通知?
【发布时间】:2017-04-19 17:50:03
【问题描述】:

使用Berlin/Tokyo和Kinvey,在收到GCM推送通知包含长消息文本时,只显示一行,其余文本被剪掉。

在网上挖了一下,好像那些通知要全长显示,需要设置BigContentView,但Delphi没有公开。

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

【问题讨论】:

    标签: android ios delphi notifications push


    【解决方案1】:

    你有两个选择:

    破解单元 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并从头开始构建自己的

    【讨论】:

    • 感谢您的提示! hack 似乎没有那么简单,因为没有定义 BigTextStyle。这是我的第一个移动应用程序,所以我需要弄清楚如何实现它。
    猜你喜欢
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多