evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 680 forks source link

Local notifications does not work on Android 7.0 if there is no mipmap/ic_launcher or drawable/ic_launcher resource or if icon resource is not named specifically in payload #761

Open alexey-m-ukolov opened 6 years ago

alexey-m-ukolov commented 6 years ago

> What version of RN and react-native-fcm are you running? react-native: 0.51.0 react-native-fcm: 11.3.1

> What device are you using? (e.g iOS9 emulator, Android 6 device)? Xiaomi Mi 5 with Android 7.0

> Is your app running in foreground, background or not running? Problem occurs regardless of app state.

I've encountered very strange problem when foreign notifications worked fine but local notifications were not displayed. Notifications from firebase console showed up in notification bar without a hitch, but FCM.presentLocalNotification({...}) with payload from example did nothing. App from examples worked fine, but my app with the same javascript didn't.

Logs from adb logcat showed this message:

01-29 18:44:25.732 25466 25491 E ReactNative: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=1 contentView=null vibrate=[0,500] sound=null defaults=0x0 flags=0x10 color=0x00000000 category=call vis=PRIVATE)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.app.NotificationManager.notifyAsUser(NotificationManager.java:306)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.app.NotificationManager.notify(NotificationManager.java:284)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.support.v4.app.NotificationManagerCompat$ImplBase.postNotification(NotificationManagerCompat.java:179)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.support.v4.app.NotificationManagerCompat.notify(NotificationManagerCompat.java:290)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.support.v4.app.NotificationManagerCompat.notify(NotificationManagerCompat.java:274)
01-29 18:44:25.732 25466 25491 E ReactNative:   at com.evollu.react.fcm.SendNotificationTask.doInBackground(SendNotificationTask.java:217)
01-29 18:44:25.732 25466 25491 E ReactNative:   at com.evollu.react.fcm.SendNotificationTask.doInBackground(SendNotificationTask.java:31)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.os.AsyncTask$2.call(AsyncTask.java:304)
01-29 18:44:25.732 25466 25491 E ReactNative:   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-29 18:44:25.732 25466 25491 E ReactNative:   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
01-29 18:44:25.732 25466 25491 E ReactNative:   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
01-29 18:44:25.732 25466 25491 E ReactNative:   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
01-29 18:44:25.732 25466 25491 E ReactNative:   at java.lang.Thread.run(Thread.java:760)

I've tried various icon fields in payload - nothing worked. Setting "com.google.firebase.messaging.default_notification_icon" also didn't help. Then I copied all mipmap directories from example and it did the trick. But notifications had my app's normal icon, not the one from example app (maybe it's just that MiUI does not support custom icons, example app also has balloon-icon, not exclamation-point-icon). My app's icon is located at @drawable/icon and AndroidManifest.xml points to it in android:icon field.

Then I deleted files copied from example app and added icon: 'icon' to payload and it worked also. And why wouldn't it?.

I'm new to mobile development so I'm not sure is it me at fault or should this library handle such cases. Anyway, I've created this issue to help anybody who will encounter the same problem I did.

evollu commented 6 years ago

"com.google.firebase.messaging.default_notification_icon" is only for remote notification created be firebase SDK. local notification is not. what is your icon file name in @drawable/icon

alexey-m-ukolov commented 6 years ago

"com.google.firebase.messaging.default_notification_icon" is only for remote notification created be firebase SDK. local notification is not.

Well, it's in a Setup Local Notifications section of the docs, so - easy mistake to make.

what is your icon file name in @drawable/icon

It's drawable-*dpi/icon.png.

Is android:icon field from AndroidManifest.xml accessible inside java classes? If so, maybe it should be the default value for small icon. For reference:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <application
            android:name=".MainApplication"
            android:allowBackup="true"
            android:label="@string/app_name"
            android:icon="@drawable/icon"
            android:theme="@style/AppTheme"
    >
        ...
    </application>
</manifest>
evollu commented 6 years ago

android:icon="@drawable/icon" is for app icon, not notification. You need to specify it in your payload for local notification.

I've updated the misleading readme by the way

alexey-m-ukolov commented 6 years ago

Yes, if I want custom icon I should specify it manually. But this library uses app icon (ic_launcher) as a fallback. But app icon could be named differently and then local notifications break completely and silently - that is the problem I've encountered. So, my proposal is to use icon specified in AndroidManifest.xml as a fallback and not rely on a hardcoded value of ic_launcher (if it's at all possible).