admin管理员组

文章数量:1289508

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.

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.

Share Improve this question edited Nov 29, 2018 at 12:10 Julien Kode 5,4991 gold badge24 silver badges38 bronze badges asked Nov 29, 2018 at 11:51 Tejas Tejas 972 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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

本文标签: javascriptHow to implement push notification with reactnativeStack Overflow