zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.76k stars 2.05k forks source link

failed to post notification on channel "fcm_fallback_notification_channel" #1852

Closed diegobonagurio closed 3 years ago

diegobonagurio commented 3 years ago

Hi Guys,

I'm trying to create a Local Push Notification, but it returns this error:

errorRN

Trying to understand the error, I think it is necessary to create a channel, following the documentation: https://github.com/zo0r/react-native-push-notification#channel-management-android, I just created with the code PushNotification.createChannel and I don't where to refer the channelId, I don't know if this is it, just a hint

Dallas62 commented 3 years ago

Hi @diegobonagurio The notification channel must be created to use local notification on Android. There is a fallback to the default Firebase channel in case it's not defined (in localNotification method) but Firebase SDK create the channel only in some case (killed app). The channelId is use in localNotification method. Regards,

diegobonagurio commented 3 years ago

Hi @Dallas62 !

So, I'm not using Firebase, just local notification.

Following what you said, where do I create this notification channel?

Dallas62 commented 3 years ago

Hi @diegobonagurio You just use the code inside the link you provided.

 PushNotification.createChannel(
    {
      channelId: "channel-id", // (required)
      channelName: "My channel", // (required)
      channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
      playSound: false, // (optional) default: true
      soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
      importance: 4, // (optional) default: 4. Int value of the Android notification importance
      vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
    },
    (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  );
diegobonagurio commented 3 years ago

@Dallas62

Will it be something like that?

import PushNotification from 'react-native-push-notification';
import {Platform} from 'react-native';

PushNotification.configure({
  // (required) Called when a remote or local notification is opened or received
  onNotification: function(notification) {
    console.log('LOCAL NOTIFICATION ==>', notification)
  },
  popInitialNotification: true,
  requestPermissions: Platform.OS === 'ios',
})

export const LocalNotification = () => {
  PushNotification.localNotification({
    channelId: "com.pushnotification",
    autoCancel: true,
    bigText:
      'This is local notification demo in React Native app. Only shown, when expanded.',
    subText: 'Local Notification Demo',
    title: 'Local Notification Title',
    message: 'Expand me to see more',
    vibrate: true,
    vibration: 300,
    playSound: true,
    soundName: 'default',
    actions: '["Yes", "No"]'
  });

  PushNotification.createChannel(
    {
      channelId: "com.pushnotification", // (required)
      channelName: "com.pushnotification", // (required)
      channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
      playSound: false, // (optional) default: true
      soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
      importance: 4, // (optional) default: 4. Int value of the Android notification importance
      vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
    },
    (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
  );
}
Dallas62 commented 3 years ago

Yes but the channel must exist before the notification is triggered

diegobonagurio commented 3 years ago

Nice @Dallas62

It's worked! 😄

Thank you!!

Balthazar33 commented 3 years ago

I'm still getting this error after adding the channel. My config:

PushNotification.createChannel(
            {  
                channelId: "my-channel", // (required)
                playSound: false, // (optional) default: true
                soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
                vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
            },
            (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
        );

        PushNotification.configure({
            // (optional) Called when Token is generated (iOS and Android)
            onRegister: function (token) {
                console.log("TOKEN:", token);
            },

            // (required) Called when a remote is received or opened, or local notification is opened
            onNotification: function (notification) {
                console.log("NOTIFICATION:", notification);

                // process the notification

                // (required) Called when a remote is received or opened, or local notification is opened
                notification.finish(PushNotificationIOS.FetchResult.NoData);
            },

            // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
            onAction: function (notification) {
                console.log("ACTION:", notification.action);
                console.log("NOTIFICATION:", notification);

                // process the action
            },

            // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
            onRegistrationError: function (err) {
                console.error(err.message, err);
            },

            // IOS ONLY (optional): default: all - Permissions to register.
            permissions: {
                alert: true,
                badge: true,
                sound: true,
            },

            // Should the initial notification be popped automatically
            // default: true
            popInitialNotification: true,

            /**
             * (optional) default: true
             * - Specified if permissions (ios) and token (android and ios) will requested or not,
             * - if not, you must call PushNotificationsHandler.requestPermissions() later
             * - if you are not using remote notification or do not have Firebase installed, use this:
             *     requestPermissions: Platform.OS === 'ios'
             */
            requestPermissions: Platform.OS === 'ios',
        });

        PushNotification.localNotificationSchedule({
            //... You can use all the options from localNotifications
            channelId: "my-channel",
            message: "My Notification Message", // (required)
            date: new Date(Date.now() + 60 * 1000), // in 60 secs
            allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
          });

And in manifest:

    <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                    android:value="false"/>
                      <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="fcm_fallback_notification_channel" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Also, createChannel returned 'false'

Platform: android RN version: 0.63.4 Package version: 7.3.1

Dallas62 commented 3 years ago

Hi @Balthazar33 Your AndroidManifest define a default channel which is probably not defined. It's returning false if the Android version is too old for channels or if channel already exist. Regards

Balthazar33 commented 3 years ago

Thanks for replying @Dallas62

Updated manifest file : (removed

<meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="fcm_fallback_notification_channel" />

)

<meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                    android:value="false"/>
     <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@color/white"/>
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
            </intent-filter>
        </receiver>

        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

Android version is 10

And the channel is not created.

Dallas62 commented 3 years ago

Hi @Balthazar33 The goal of com.google.firebase.messaging.default_notification_channel_id is to define the default channel. Removing it will not change the value since it's the default value of Firebase. https://github.com/zo0r/react-native-push-notification#channel-management-android

For local notifications, the same kind of option is available:

you can use:

  <meta-data
     android:name="com.dieam.reactnativepushnotification.default_notification_channel_id"
     android:value="@string/default_notification_channel_id" />

If not defined, fallback to the Firebase value defined in the AndroidManifest:

  <meta-data
      android:name="com.google.firebase.messaging.default_notification_channel_id"
      android:value="..." />

If not defined, fallback to the default Firebase channel id fcm_fallback_notification_channel

Where do you look to know if the channel is created or not ?

Balthazar33 commented 3 years ago

Where do you look to know if the channel is created or not ?

In the console. When I build the app for the first time, it says false. I know that it will be true just once and false thereafter once it has been created, but I'm not able to see it return 'true' even once.

Dallas62 commented 3 years ago

You need to look inside parameters of the phone. Channels are not deleted when the application is uninstall / resinstalled. This is required to avoid abuses from applications (delete / create channel to reset channel preferences).

Balthazar33 commented 3 years ago

You need to look inside parameters of the phone. Channels are not deleted when the application is uninstall / resinstalled. This is required to avoid abuses from applications (delete / create channel to reset channel preferences).

PushNotification.channelExists("my-channel", function (exists) {
            console.log(exists); // true/false
          });

returns false

Balthazar33 commented 3 years ago

I started receiving the notifications once I updated the code to :

PushNotification.createChannel(
            {
              channelId: "my-channel", // (required)
              channelName: "My channel", // (required)
              channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
              playSound: false, // (optional) default: true
              soundName: "default", // (optional) See `soundName` parameter of `localNotification` function
              vibrate: true, // (optional) default: true. Creates the default vibration patten if true.
            },
            (created) => console.log(`createChannel returned '${created}'`) // (optional) callback returns whether the channel was created, false means it already existed.
          );

I thought the channel name and description fields were deprecated, and hence didn't add those earlier.