How to implement push notification with react-native

牧云@^-^@ 提交于 2020-12-06 04:28:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!