繁体   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