2

私はreact-navigationreact-native-push-notificationを使用しています。 onNotificationコールバックで特定のStackNavigator's画面を開くにはどうすればよいですか?ときに動作するはずです:アプリが閉じているReact Native - プッシュ通知からルートを開く方法

  • アプリがフォアグラウンドで
  • アプリは、私が唯一それが今のAndroidの中で働いて必要な背景

になっています。

私は私のコンポーネントに通知するコールバック関数を渡すために試してみた:

_handleClick() { 
    PushNotification.localNotification({ 
    foreground: false 
    userInteraction: false 
    message: 'My Notification Message' 
    onOpen:() => { this.props.navigation.navigate("OtherScreen") }, 
    }) 
} 

そしてPushNotification設定でonOpenを発射する:

onNotification: function(notification) { 
    notification.onOpen() 
} 

しかし、機能はすることはできませんようです値が文字列でない限り、通知に渡されます。onOpenは定義されません。私は自分のソリューションを投稿奨めよう

答えて

3

さて、それはそうです:)

// src/services/push-notification.js 
const PushNotification = require('react-native-push-notification') 

export function setupPushNotification(handleNotification) { 
    PushNotification.configure({ 

     onNotification: function(notification) { 
     handleNotification(notification) 
     }, 

     popInitialNotification: true, 
     requestPermissions: true, 
    }) 

    return PushNotification 
} 


// Some notification-scheduling component 
import {setupPushNotification} from "src/services/push-notification" 

class SomeComponent extends PureComponent { 

    componentDidMount() { 
    this.pushNotification = setupPushNotification(this._handleNotificationOpen) 
    } 

    _handleNotificationOpen =() => { 
    const {navigate} = this.props.navigation 
    navigate("SomeOtherScreen") 
    } 

    _handlePress =() => { 
    this.pushNotification.localNotificationSchedule({ 
     message: 'Some message', 
     date: new Date(Date.now() + (10 * 1000)), // to schedule it in 10 secs in my case 
    }) 

    } 

    render() { 
    // use _handlePress function somewhere to schedule notification 
    } 

} 
関連する問題