问题
Socket.io channel disconnects whenever the app is in background or closed, how to keep channel connected all time because i have to include push notification feature to my chat application.
回答1:
What is the best way to handle notifications on react-native
It depends on the platform that you support, to receive mobile push notification on the device the best way is to implement the platform specific solution.
Why do you do that ?
Because even if you app is close or in background, the user will get notification on the screen and you can handle it
For iOS:
You have use an APNs server to send push notification
For Android:
You have to use GCM
To make the implementation of that even easier you can use services like:
- Urban Airship
- Amazon SNS
- Pusher
- OneSignal
- Firebase
How to implement that with react-native ?
You have really good libraries to do that:
With react-native-push-notification
here is an example with this library:
var PushNotification = require('react-native-push-notification');
PushNotification.configure({
onRegister: function(token) {
// Call when your device register the token
// You have to pass this token to the API or services like OneSignal, Pusher etc...
console.log( 'TOKEN:', token );
},
// This is trigger when a remote notification is received or open
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
},
// This is only for Android
senderID: "YOUR GCM (OR FCM) SENDER ID",
// This is only for iOS
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
// Ask the permission to receive notifications
requestPermissions: true,
});
You also have libraries that implement the service of your choice like :
- Pusher: react-native-pusher-push-notifications
- OneSignal: react-native-onesignal
- Firebase: react-native-firebase
I hope my answer help you 😊
来源:https://stackoverflow.com/questions/53538428/how-to-implement-push-notification-with-react-native