使用Xamarin Form 上架Android APP無法通過API層級31以上的測試

張爸爸 0 Reputation points
2023-05-17T01:06:24.2133333+00:00
使用Xamarin Form 開發的APP,上傳至Google 審查測試時出現以下錯誤:
Exception java.lang.IllegalArgumentException: com.yzu.XamarinPrism: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at 

試圖使用以下方法處理,但仍無法解決。
1.安裝Xamarin.Androidx.Work.Runtime
2.增加POST_NOTIFICATIONS的授權
3.修改 PendingIntent的版本判斷,PendingIntentFlags.Immutable
if (System.Convert.ToInt16(Build.VERSION.SdkInt)>=23)
            {
                return PendingIntent.GetActivity(context, id, intent, PendingIntentFlags.Immutable);
            }
            else
            {
                return PendingIntent.GetActivity(context, id, intent, flag);
            }

請問是否有其它的解決方法,或是其它資訊可提供參考,謝謝。


Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,975 questions
{count} votes

1 answer

Sort by: Most helpful
  1. 張爸爸 0 Reputation points
    2023-05-26T03:05:35.8966667+00:00
    1.更新Xamarin.FireBase.Messaging 套件至適合自己專案的版本,我的例子是從(60.1142.1->71.1740.0) ,因為一次更新到最新版,會造成專案錯誤。
    2.加入必要權限授權(POST_NOTIFICATION)
    3.在FirebaseMsgingService.cs中加入以下code
    
    public static PendingIntent createPendingIntentGetActivity(Context context, int id, Intent intent, PendingIntentFlags flag)
            {
                //Android 6.0 (API level 23)
                if (System.Convert.ToInt16(Build.VERSION.SdkInt) >= 23)
                {
                    return PendingIntent.GetActivity(context, id, intent, PendingIntentFlags.Immutable);
                }
                else
                {
                    return PendingIntent.GetActivity(context, id, intent, flag);
                }
            }
    
    4.在FirebaseMsgingService.cs中SendNotification()中加入
    var intent = new Intent(this, typeof(MainActivity));
    var pendingIntent = createPendingIntentGetActivity(this, 0, intent, PendingIntentFlags.Immutable);
    
    
    0 comments No comments