首页 > 解决方案 > 如何使用 react-native 实现推送通知

问题描述

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

标签: javascriptandroidiosreact-nativepush-notification

解决方案


在 react-native 上处理通知的最佳方法是什么

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

你为什么这样做?

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

对于 iOS:

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

对于安卓:

你必须使用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,
});

您还拥有实现您选择的服务的库,例如:

希望我的回答对你有帮助


推荐阅读