P. Vitvera
P. Vitvera

Reputation: 95

Unity firebase notifications custom image

I have decided to implement Firebase notifications into my unity project. The issue is that for notifications I want to use custom image, but that image is also used as app image (app_icon). I tried to modify manifest where I put into MessagingUnityPlayerActivity activity custom notification image (notificon) for displaying notification icon.

<?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>

I tried to modify it and delete some tags, but then the build wond compile. I should also mention that I have multiple manifests in my project and tried them to merge together but unsuccessfully. But this shouldnt be the problem since this is the only manifest with android:icon propery.

Thank you

Upvotes: 5

Views: 6026

Answers (4)

Roman
Roman

Reputation: 41

Then change the ZIP file extension to AAR in the explorer. You can then specify the icons name from the senders side. More details of the AAR anatomy here.

If you just change the extension to aar, it won't compile.

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-file must be created in Android Studio. Since it must contain a number of required files such as AndroidManifest.xml, classes.jar, etc.

Upvotes: 0

Codehai
Codehai

Reputation: 534

Putting a res folder inside Assets/Plugins/Android using AAB is obsolete and will prevent you from building.

Since I was too lazy to install Android Studio to build a custom AAR I solved it by placing a ZIP-Archive into the Assets/Plugins/Android folder, with the res folder inside (res folder created with this as mentioned in an other answer) and the following snippet inside a AndroidManifest.xml next to the res folder:

<?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>

Then change the ZIP file extension to AAR in the explorer. You can then specify the icons name from the senders side. More details of the AAR anatomy here.

Upvotes: 2

Anh Ngọc
Anh Ngọc

Reputation: 141

  1. Make an image (a white on transparent version of game icon, png) under folder "Assets\Plugins\Android\res\drawable.
  2. In my case, the name of my image is "small_icon.png" then in "Assets\Plugins\Android\AndroidManifest.xml", add it after opening tag:

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

Your AndroidManifest.xml should be like

<?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>
....

Upvotes: 5

MGGD
MGGD

Reputation: 41

No need to change the manifest. This worked for me:

So you should be able to add your icon(s) to Assets/Plugins/Android/res/drawable/ and reference it name

Then, in the notification json, specify your icon. For example:

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

Note: I used one of these icons.

Upvotes: 0

Related Questions