簡體   English   中英

unity firebase 通知自定義圖片

[英]Unity firebase notifications custom image

我決定在我的統一項目中實施 Firebase 通知。 問題是對於通知我想使用自定義圖像,但該圖像也用作應用程序圖像 (app_icon)。 我試圖修改我放入 MessagingUnityPlayerActivity 活動自定義通知圖像 (notificon) 以顯示通知圖標的清單。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="${applicationId}"
          android:versionCode="1"
          android:versionName="1.0">
  <application android:label="@string/app_name"
               android:icon="@drawable/app_icon">
    <!-- The MessagingUnityPlayerActivity is a class that extends
         UnityPlayerActivity to work around a known issue when receiving
         notification data payloads in the background. -->
    <activity android:name="com.google.firebase.MessagingUnityPlayerActivity"
              android:label="@string/app_name"
              android:icon="@drawable/notificon"
              android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <service android:name="com.google.firebase.messaging.MessageForwardingService"
             android:exported="false"/>
  </application>

</manifest>

我試圖修改它並刪除一些標簽,但隨后構建無法編譯。 我還應該提到我的項目中有多個清單並嘗試將它們合並在一起但沒有成功。 但這不應該是問題,因為這是唯一具有 android:icon 屬性的清單。

謝謝

使用 AAB 將res文件夾放在Assets/Plugins/Android已經過時,並且會阻止您構建。

由於我懶得安裝 Android Studio 來構建自定義 AAR,我通過將 ZIP-Archive 放入Assets/Plugins/Android文件夾中解決了這個問題,其中包含res文件夾(res 文件夾是用這個創建的,如另一個答案中所述)以及res文件夾旁邊的AndroidManifest.xml中的以下代碼段:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
package="com.google.firebase.messaging" 
android:versionCode="1" 
android:versionName="1.0">
<application>
<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/YOURICONIMAGENAMEWITHOUTEXTENSION" />
        </application>
</manifest>

然后在資源管理器中將ZIP文件擴展名改成AAR。 然后您可以從發件人端指定圖標名稱。 有關 AAR 剖析的更多詳細信息,請參見 此處

無需更改清單。 對我有用:

所以你應該能夠將你的圖標添加到Assets / Plugins / Android / res / drawable /並引用它的名字

然后,在通知json中,指定您的圖標。 例如:

"notification": {
        "title": "Title",
        "body" : "First Notification",
        "text" : "Text",
        "icon" : "MyIcon"
    }

注意:我使用了其中一個圖標

  1. 在“Assets \\ Plugins \\ Android \\ res \\ drawable”文件夾下制作一個圖像(透明版游戲圖標上的白色,png)。
  2. 在我的例子中,我的圖像名稱為“small_icon.png”,然后在“Assets \\ Plugins \\ Android \\ AndroidManifest.xml”中,在打開標記后添加它:

<meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/small_icon" />

你的AndroidManifest.xml應該是這樣的

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" package="${applicationId}" 
android:versionCode="1" android:versionName="1.0">
  <application android:label="@string/app_name" android:icon="@drawable/app_icon">

<meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/small_icon" />
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
....

然后在資源管理器中將ZIP文件擴展名改成AAR。 然后您可以從發件人端指定圖標名稱。 有關 AAR 剖析的更多詳細信息,請參見 此處

如果你只是將擴展名更改為 aar,它將無法編譯。

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':unityLibrary:compileReleaseJavaWithJavac'.
> Could not resolve all files for configuration ':unityLibrary:releaseCompileClasspath'.
   > Failed to transform firebase_notification_icon-.aar (:firebase_notification_icon:) to match attributes {artifactType=android-classes-jar, org.gradle.status=integration, org.gradle.usage=java-api}.
      > Execution failed for AarToClassTransform: C:\...\.gradle\caches\transforms-2\files-2.1\ea6ac2b3d0993f0958d52cbbdfbd76ea\jetified-firebase_notification_icon.aar.
         > entry

AAR 文件必須在 Android Studio 中創建。 由於它必須包含一些必需的文件,例如AndroidManifest.xml、classes.jar等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM