zo0r / react-native-push-notification

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

Foreground not working (app close , app background) #1718

Open michaelVictoriaDev opened 4 years ago

michaelVictoriaDev commented 4 years ago

my index.js

import { AppRegistry, Platform } from 'react-native';
import App from './src/App';
import { name as appName } from './app.json';
import PushNotification from 'react-native-push-notification';
import NavigationService from "./src/components/navigationService"
AppRegistry.registerComponent(appName, () => App);

 // Must be outside of any component LifeCycle (such as `componentDidMount`).
 PushNotification.configure({
    smallIcon: "ic_notification",

    onRegister: function (token) {
        console.log("TOKEN:", token);
    },

    onNotification: function (notification) {
        console.log("NOTIFICATION:", notification);

        NavigationService.navigate('OrderInfoNotif', {  orderId: notification.tag,});
    },

    permissions: {
        alert: true,
        badge: true,
        sound: true,
    },

    popInitialNotification: true,

    requestPermissions: false,
});

my notification.js trigger this

 sendMessage = (notifMessage, order_id) => {
        console.log('notifMessage', notifMessage)
        console.log('order_id', order_id)
        var channel
        PushNotification.getChannels(function (channel_ids) {
            console.log(channel_ids); // ['channel_id_1']
            channel = channel_ids
            console.log('channel', channel_ids)
        });
        PushNotification.channelExists(channel, function (exists) {

            console.log(exists); // true/false
        });

        PushNotification.localNotification({
            channelId: _.toString(channel),// (required)
            message: `${notifMessage}`, // (required)
            soundName: "notif",
            smallIcon: "icon",
            vibrate: true,
            vibration: 300,
            number: 10,
            playSound: true,
            tag: order_id,
            ignoreInForeground: false,

        });

    }

AndroidManifest.xml looks like this.

<!-- 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" />

but the foreground is not working.

Dallas62 commented 4 years ago

Hi,

The way you use channel id is incorrect. Please define a unique id of channel. _.toString of an array doesn't make sense.

Regards

michaelVictoriaDev commented 3 years ago

Hi @Dallas62 Actually the localnotification when the app is active is trigger, but when in background is not trigger, .

Dallas62 commented 3 years ago

Follow the Channel documentation, this will probably solve your issue.

michaelVictoriaDev commented 3 years ago

My firebase structure is looks like this, so basically when there's notification or order all that value of objects will be changed,

image

My localnotification snippet looks like this, I listened the timestamp when there's changed in the firebase, so basically the createChannel is not needed anymore cause I have an order_id equivalent to channel, I used the number-to-words library so it will be string words not numbers. but still the foreground is not happening.

sendMessage = (notifMessage, order_id) => {
        console.log('notifMessage', notifMessage)
        console.log('order_id', order_id)
        var channel

        PushNotification.createChannel(
            {
                channelId: converter.toWords(order_id), // (required)
                channelName: converter.toWords(order_id), // (required)
                // channelDescription: "A channel to categorise your notifications", // (optional) default: undefined.
                soundName: "notif", // (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.
        );

        PushNotification.localNotification({
            channelId: converter.toWords(order_id),// (required)
            message: `${notifMessage}`, // (required)
            soundName: "notif",
            smallIcon: "icon",
            vibrate: true,
            vibration: 300,
            number: 10,
            playSound: true,
            tag: order_id,
            // ignoreInForeground: false,

        });

    }
arunachalam-b commented 3 years ago

Make sure you follow this documentation https://github.com/react-native-push-notification-ios/push-notification-ios

react-native-push-notification is dependent on this library for IOS (@react-native-community/push-notification-ios)