evollu / react-native-fcm

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

FCM.presentLocalNotification not working with react-navigation #1106

Closed canerd7u closed 4 years ago

canerd7u commented 5 years ago

i have project in react navigation and i want use push notification send post request.

my app.js

`import React ,{ Component } from "react"; import { View, Text , Button , Platform , TextInput } from "react-native"; import { createStackNavigator, createAppContainer } from "react-navigation";

import FCM, { FCMEvent, WillPresentNotificationResult, NotificationType, } from "react-native-fcm";

import firebaseClient from "./FirebaseClient";

class HomeScreen extends React.Component { static navigationOptions = { title: 'Welcome', }; render() { const {navigate} = this.props.navigation; return ( <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>

Home Screen
            <Button
                title="Go to Jane's profile"
                onPress={() => navigate('TwoHome', {name: 'Jane'})}
            />
        </View>
    );
}

}

FCM.on(FCMEvent.Notification, notif => { console.log("Bildirim geldi", notif);

if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
    // Bu bildirim yalnızca kullanıcı ön planda ise bildirimi göstermek isteyip istemediğinize karar vermektir.
    // genellikle görmezden gelebilirsiniz. sadece göster/gösterme yapar.
    notif.finish(WillPresentNotificationResult.All);
    return;
}

alert(JSON.stringify(notif));

FCM.presentLocalNotification({
    id: new Date().valueOf().toString(),         // (optional for instant notification)
    title: "Test Notification with action",      // as FCM payload
    body: "Force touch to reply",                // as FCM payload (required)
    show_in_foreground: true                     // notification when app is in foreground (local & remote)
});

});

FCM.getInitialNotification().then(notification => { console.log("notification" , notification); });

// eğer token yenilenirse çalışır FCM.on(FCMEvent.RefreshToken, token => { console.log("TOKEN YENİLENDİ (refreshUnsubscribe)", token); });

// Uygulamaya Push atmak için izin alıyoruz try { FCM.requestPermissions({badge: false, sound: true, alert: true}); } catch(e){ console.error(e); }

// Firebase Cloud Message Token değerini getirir. FCM.getFCMToken().then(token => { console.log("TOKEN (getFCMToken)", token); global.token = token; });

if(Platform.OS === 'ios') { // Eğer APNS istiyorsanız isteğe bağlı APNS TOKEN FCM.getAPNSToken().then(token => { console.log("APNS TOKEN (getAPNSToken)", token); }); }

class TwoScreen extends React.Component {

sendRemoteNotification() {

    let body;

    token = global.token;

    if (Platform.OS === "android") {
        body = {
            to: token,
            data: {
                custom_notification: {
                    title: "Simple FCM Client",
                    body: "Click me to go to detail ses",
                    sound: "default",
                    priority: "high",
                    show_in_foreground: true,
                    targetScreen: "detail"
                }
            },
            priority: 10
        };
    } else {
        body = {
            to: token,
            notification: {
                title: "Simple FCM Client",
                body: "Click me to go to detail",
                sound: "default"
            },
            data: {
                targetScreen: "detail"
            },
            priority: 10
        };
    }

    firebaseClient.send(JSON.stringify(body), "notification");
}

render() {
    return (
        <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
            <Text>Home Screen</Text>
            <Button title="Send" onPress={this.sendRemoteNotification} />
            <TextInput> {global.token} </TextInput>
        </View>
    );
}

}

const AppNavigator = createStackNavigator({ Home: { screen: HomeScreen } , TwoHome: { screen: TwoScreen } });

export default createAppContainer(AppNavigator);`

edritech93 commented 5 years ago

same issue for me

lexdorf-17 commented 4 years ago

hai i have same issue, @evollu any update for this problem ?

canerd7u commented 4 years ago

@dedenhendrap , @edritech93

`{ "to":"".state['kullanici_fcm_token']."", "data":{ "sound":"default", "priority":"high", "show_in_foreground":true, "content_available":true, "bildirim_ref":"user",

}, "notification":{ "title":"Satış İzni", "body":" $title ", "content_available":true }, "priority":10 }`

use same json with remote notification. for me success.