如何使用React Native实现推送通知

5

当应用程序处于后台或关闭状态时,Socket.io频道会断开连接,如何始终保持通道连接,因为我必须将推送通知功能包含在我的聊天应用程序中。

1个回答

9

如何在React Native上处理通知是最好的方式?

这取决于您支持的平台,要在设备上接收移动推送通知,最好的方法是实现特定于平台的解决方案。

为什么要这样做?

因为即使您的应用程序关闭或在后台运行,用户也会在屏幕上收到通知,您可以处理它。

对于iOS:

您必须使用APNs服务器发送推送通知

对于Android:

您必须使用GCM

为了使实现更加容易,您可以使用以下服务:

如何在react-native中实现?

您有非常好的库可以完成此操作:

使用react-native-push-notification

这是一个使用此库的示例:

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,
});

您还可以使用实现所选服务的库,例如:

希望我的回答对您有所帮助。


@JulienKode,你能告诉我如何在用户点击通知时触发某些操作吗? - Kishan Bharda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接