invertase / react-native-firebase

🔥 A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.71k stars 2.22k forks source link

Can not override notification_color in AndroidManifest.xml #3559

Closed tautvilas closed 4 years ago

tautvilas commented 4 years ago

If i try to set notification icon color (according to official firebase docs https://firebase.google.com/docs/cloud-messaging/android/receive):

<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/colorAccent" />

When building Android app I get this error:

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#com.google.firebase.messaging.default_notification_color@resource value=(@color/main) from AndroidManifest.xml:45:11-41
        is also present at [:react-native-firebase_messaging] AndroidManifest.xml:42:13-44 value=(@color/white).
        Suggestion: add 'tools:replace="android:resource"' to <meta-data> element at AndroidManifest.xml:43:9-45:44 to override

react-native-firebase version: 6.7.1

This option used to work with react-native-firebase v5

Ehesp commented 4 years ago

https://rnfirebase.io/messaging/usage#notification-color

tautvilas commented 4 years ago

Thanks

b3coded commented 4 years ago

@Ehesp Can I use a custom color added in colors.xml (eg. @color/primary) ?

Ehesp commented 4 years ago

Yeah you just need to make sure the resource is accessible. Try adding it via studio.

b3coded commented 4 years ago

I tried but I got this error: AndroidManifest.xml:40: AAPT: error: resource color/primary (aka io.invertase.firebase.messaging:color/primary) not found.

My RNFirebase deps: { "@react-native-firebase/analytics": "^7.1.7", "@react-native-firebase/app": "^8.0.0", "@react-native-firebase/crashlytics": "^8.0.1", "@react-native-firebase/dynamic-links": "^7.2.2", "@react-native-firebase/in-app-messaging": "^7.1.7", "@react-native-firebase/messaging": "^7.2.1", "@react-native-firebase/perf": "^7.1.7", }

My colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="primary">#8CC83C</color>
  <color name="dark">#000000</color>
  <color name="light">#FFFFFF</color>
  <color name="primary_dark">#000000</color>
  <color name="bootsplash_background">#8CC83C</color>
</resources>
mingyjongo commented 4 years ago

@b3coded I'm experiencing the same. I know I was able at some point to build successfully by setting my color in firebase.json, but it is failing for me now with the same error as yours.

Deps ```json "dependencies": { "@react-native-firebase/app": "^7.3.0", "@react-native-firebase/messaging": "^7.1.7" } ```

I am currently able to move past this by overriding directly in my AndroidManifest.xml as follows:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_color"
    android:resource="@color/primary"
    tools:replace="android:resource"
/>
devoto13 commented 4 years ago

I can't use a custom colour using the firebase.json file approach either and I don't understand how it may work.

The value from firebase.json will replace a placeholder in node_modules/@react-native-firebase/messaging/android/src/main/AndroidManifest.xml as part of the build of the react-native-firebase_messaging library. But react-native-firebase_messaging library (a dependency of the application) has not idea about a resource defined in the application (and it should not). It expectedly fails during :react-native-firebase_messaging:verifyReleaseResources task (only run for Release builds) as its AndroidManifest.xml references a colour, which is not defined in resources visible to react-native-firebase_messaging package. The approach from https://github.com/invertase/react-native-firebase/issues/3559#issuecomment-654380585 looks like a correct solution to the problem.

So as I see it consumers can only use colours defined in node_modules/@react-native-firebase/messaging/android/src/main/res/values/colors.xml in the firebase.json file.

mikehardy commented 4 years ago

So as I see it consumers can only use colours defined in node_modules/@react-native-firebase/messaging/android/src/main/res/values/colors.xml in the firebase.json file.

@devoto13 that is what the statement on the reference page https://rnfirebase.io/messaging/usage#notification-color is supposed to mean:

The library provides a set of default HTML colors (in lowercase) for ease, for example

So if it's not in this list https://github.com/invertase/react-native-firebase/blob/master/packages/messaging/android/src/main/res/values/colors.xml you can't access it without using patch-package to patch in there

If the documentation could be made clearer (and documentation can almost always be made more clear!) please hit the edit link on the top right of that page and propose a PR :-)

devoto13 commented 4 years ago

@mikehardy I see. It it was not at all clear from the current text that it is not possible to use custom colour this way... Especially after reading https://github.com/invertase/react-native-firebase/issues/3559#issuecomment-652643494, which seems to claim otherwise. Let me try to add an example of how to use a custom color to the documentation page.

papuruth commented 3 years ago

@b3coded I'm experiencing the same. I know I was able at some point to build successfully by setting my color in firebase.json, but it is failing for me now with the same error as yours.

Deps I am currently able to move past this by overriding directly in my AndroidManifest.xml as follows:

<meta-data
  android:name="com.google.firebase.messaging.default_notification_color"
  android:resource="@color/primary"
  tools:replace="android:resource"
/>

I'm using this approach but still, the notification icon is colored white.

harsh-kukreja commented 3 years ago

Any update on this issue?

@papuruth were you able to solve it?

jack828 commented 3 years ago

Just posting for visibility... If you're experiencing this error message yet you've followed all the steps outlined (replacement in AndroidManifest.xml), please make sure that past you didn't try and do this and amend firebase.json - you can have only one!

mikehardy commented 3 years ago

Interesting! Perhaps the configuration machinery, in debug builds, could generate a warning if firebase.json would configure something but determines it would be a collision :thinking:

levim770 commented 2 years ago

@b3coded I'm experiencing the same. I know I was able at some point to build successfully by setting my color in firebase.json, but it is failing for me now with the same error as yours.

Deps I am currently able to move past this by overriding directly in my AndroidManifest.xml as follows:

<meta-data
  android:name="com.google.firebase.messaging.default_notification_color"
  android:resource="@color/primary"
  tools:replace="android:resource"
/>
thanks, just don't forget to add this line top of the Manifest: 

"<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yourpackage" xmlns:tools="http://schemas.android.com/tools">" <---- add this line

anurag55555 commented 2 years ago

I have tried all these things but the issue I am facing is when the app is in the background the notification icon colour is different from what I am passing via. the manifest file.

For example, suppose I have passed green colour in the notification icon so if I got any notification when my phone screen is off the notification icon colour will be white in dark mode and grey in light mode. To tackle that I passed colour in firebase.json as mentioned in documentation which got applied in the situation mentioned in example.

But the issue is, I am not able to pass custom colours other than predefined colours. Is there any way to make this happen other than firebase.json since as mentioned above we cannot add custom colour in firebase.json

mikehardy commented 2 years ago

Possibly? If you dive in to the code to see how firebase.json is used, you'll see we are just interpolating those items in to AndroidManfiest.xml via manifest placeholders.

https://github.com/invertase/react-native-firebase/blob/dbc79c31810bf9bea4c3a782432c9b5625ad3d5f/packages/messaging/android/src/main/AndroidManifest.xml#L34

According to the manifest layering rules of android packaging, any elements in libraries will be overridden by elements in your main AndroidManfest, which will in turn be overridden by any other manifests you layer on top (via flavors or similar).

So if you include the correct configuration element in your AndroidManifest.xml in the application it really should take. You can open your .apk in Android Studio and see the merged manifest, or perhaps just open the AndroidManifest for your app to see how it is actually configured https://search.brave.com/search?q=how+to+see+merged+manifest+in+android+studio&source=desktop

anurag55555 commented 2 years ago

So if you include the correct configuration element in your AndroidManifest.xml in the application it really should take. You can open your .apk in Android Studio and see the merged manifest, or perhaps just open the AndroidManifest for your app to see how it is actually configured https://search.brave.com/search?q=how+to+see+merged+manifest+in+android+studio&source=desktop

I have added colour in the android manifest file like below:

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

    <meta-data android:name="com.dieam.reactnativepushnotification.notification_color" android:resource="@color/brand_color"/>

looked into merged manifest which also has this : <meta-data 79-->/home/anuragwalia/Documents/stanza-super-app-new/android/app/src/main/AndroidManifest.xml:14:5-127 80 android:name="com.dieam.reactnativepushnotification.notification_color" 80-->/home/anuragwalia/Documents/stanza-super-app-new/android/app/src/main/AndroidManifest.xml:14:16-87 81 android:resource="@color/brand_color" />

so I think its probably taking the colour that is why when the app is in the foreground brand colour is getting applied only when the phone light is off and you receive notification, in that case, it's not getting applied

mikehardy commented 2 years ago

I think that's unrelated, but at same time, no idea why you'd use react native push notification at the same time you use react-native-firebase/messaging, they're both trying to handle FCM delivery, that sounds like it won't go well, but...who knows? Not a supported configuration really. That said, examining your merged manifest, and making sure the keys are correct + what you expect is the way. Note that you can remove keys by specifying them with remove elements in your app level AndroidManifest. None of that is really in scope for support here though - we try to keep the issues list here focused on things that will result in a change in the module and this is pretty project-specific. I suggest following up on stack overflow as needed

santhosh-umapathi-appymango commented 1 year ago

Thanks, this was the missing piece, it worked after added the xmlns:tools="http://schemas.android.com/tools" on the top

samuelcastrok commented 1 year ago

@santhosh-umapathi-appymango did you manage to change the color of your icon? I tried to put xmlns:tools="http://schemas.android.com/tools but without success...

santhosh-umapathi-appymango commented 1 year ago

@samuelcastrok , on the latest android version, I was able to get my notification icon with all colors in it (I used the original image). On the older versions, I used single color and it worked.

ianitsky commented 1 year ago

@samuelcastrok , this change don't work on the time, you need to wait a little. It's not working for me, but after one hour I tested again and everything is ok.