zo0r / react-native-push-notification

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

Notification not show when app is in foreground, background or closed #1712

Closed Piero87 closed 4 years ago

Piero87 commented 4 years ago

Bug

Hello, I'm using this library to receive push notification in my react native app, on iOS all working fine instead on Android I don't see any notification but I see only in the terminal with a console log inside onNotification callback, I show you how:

onNotification: notification => {
        console.log("NOTIFICATION:", notification);
      },

And in the console I see this:

NOTIFICATION: {"data": {"badge": "1", "message": "test test test", "sound": "default"}, "finish": [Function finish], "foreground": true, "id": "1319931916", "userInteraction": false}

or this:

NOTIFICATION: {"data": {"badge": "1", "message": "test test test", "sound": "default"}, "finish": [Function finish], "foreground": false, "id": "-1095461832", "userInteraction": false}

But in the device when the app is open, in background or closed I don't see any banner or icon nothing. I searched a lot in the issues of this library and I find something similar like: https://github.com/zo0r/react-native-push-notification/issues/1496 or https://github.com/zo0r/react-native-push-notification/issues/1395, all talking about setup a channel, I have tried also that with no luck. I show you my code:

PushNotification.configure({
      senderID: 'xxxxxxxxx',
      onRegister: ....
      onNotification: ....
      onRegistrationError: ....
      onAction: ...
      permissions: {
        alert: true,
        badge: true,
        sound: true,
      },
      popInitialNotification: true,
      requestPermissions: true,
});

AndroidManifest.xml

        <!-- Change the value to true to enable pop-up for in foreground (remote-only, for local use ignoreInForeground) -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
                    android:value="true"/>

        <meta-data
          android:name="com.google.firebase.messaging.default_notification_channel_id"
          android:value="rn-push-notification-channel-id-4-300" />
        <meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_description" android:value="TVFilesBeta"/>

        <!-- Change the resource name to your App's accent color - or any other color you want -->
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@color/background"/> <!-- or @android:color/{name} to use a standard color -->

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

Can anyone help me please? I'm going crazy!

Environment info

"@react-native-community/push-notification-ios": "^1.5.0", "react-native-push-notification": "^6.1.1", "react-native": "^0.63.3",

Dallas62 commented 4 years ago

Hi, Has you may notice in the Readme, you must create a channel in order to receive / trigger notifications. Regards

Piero87 commented 4 years ago

Hello @Dallas62, I'm sorry I miss that part in my first comment, I already tried also that, I do this:

      onRegister: async data => {
        console.log("TOKEN:", data);

        try {

            if (Platform.OS === 'android') {

              PushNotification.createChannel(
                {
                  channelId: "rn-push-notification-channel-id-4-300", // (required)
                  channelName: "Beta Android Push", // (required)
                  channelDescription: "Beta Android Push", // (optional) default: undefined.
                  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.
              );
          }
        } catch (error) {
          console.log(error);
        }
      },

But it's not working, do the same, only notification from the console log and nothing on the device

Dallas62 commented 4 years ago

You are sending data-only notification, this will not trigger a visible notification. Please refer to the Firebase documentation for the notification payload. Regards

Piero87 commented 4 years ago

@Dallas62 Yes! it was that! thank you very much! About the channel name is arbitrary? I can choose my own just by changing your default one? I have also another question, my implementation of PushNotification.createChannel is correct?

Dallas62 commented 4 years ago

You can use your own values for each parameter, keep then meaningful for the user. The channel can be created at application startup, you are not forced to wait for registration or filter by OS.

Piero87 commented 4 years ago

@Dallas62 Thank you very much!

xsats commented 3 years ago

You are sending data-only notification, this will not trigger a visible notification. Please refer to the Firebase documentation for the notification payload. Regards

I think I have the same problem (data-only notification meaning no visible notification), but I can't seem to find info on what extra information to add to the payload to rectify this. EDIT - found it here

I'm also interested in which node module is best to use to trigger remote push notifications on the server with FCM. At the moment I'm using the sendMulticast() method of node firebase-admin - is this what you would recommend? Many thanks in advance

EDIT: I went poking around in the types (should've done so straight away), and have found that the payload should have the following structure:

interface BaseMessage {
        data?: {
            [key: string]: string;
        };
        notification?: Notification;
        android?: AndroidConfig;
        webpush?: WebpushConfig;
        apns?: ApnsConfig;
        fcmOptions?: FcmOptions;
}

plus the tokens array.

Unfortunately, even with this I only get the data message and not a notification

xsats commented 3 years ago

You are sending data-only notification, this will not trigger a visible notification. Please refer to the Firebase documentation for the notification payload. Regards

I think I have the same problem (data-only notification meaning no visible notification), but I can't seem to find info on what extra information to add to the payload to rectify this. EDIT - found it here

I'm also interested in which node module is best to use to trigger remote push notifications on the server with FCM. At the moment I'm using the sendMulticast() method of node firebase-admin - is this what you would recommend? Many thanks in advance

EDIT: I went poking around in the types (should've done so straight away), and have found that the payload should have the following structure:

interface BaseMessage {
        data?: {
            [key: string]: string;
        };
        notification?: Notification;
        android?: AndroidConfig;
        webpush?: WebpushConfig;
        apns?: ApnsConfig;
        fcmOptions?: FcmOptions;
}

plus the tokens array.

Unfortunately, even with this I only get the data message and not a notification

Seems as though I have the same behaviour as #1602 - likely related to the fact the device I'm using for testing is running Android 7 (unable to update).