【问题标题】:How to implement push notification with react-native如何使用 react-native 实现推送通知
【发布时间】:2019-05-01 11:33:11
【问题描述】:

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

【问题讨论】:

    标签: javascript android ios react-native push-notification


    【解决方案1】:

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

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

    希望我的回答能帮到你?

    【讨论】:

    • @JulienKode 你能告诉我当用户点击通知时如何触发一些动作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多