菜鸟教程小白 发表于 2022-12-13 05:37:40

android - React Native 中的推送通知


                                            <p><p>我正在借助“react-native-push-notification”库将推送通知集成到 react native 中。推送来自节点 js(后端)。
我在 Android 和 IOS 环境中都成功推送。</p>

<p>现在我有两个问题
1)在ios中,当我收到推送时,通知有效负载“userInteraction”中有一个参数。此参数用于该用户是否单击通知。现在它在 android 中运行良好,但在 ios 中这总是错误的。
2)我想在推送通知中设置自定义图像,这是来自推送。</p>

<p>我正在使用“<a href="https://www.npmjs.com/package/react-native-push-notification" rel="noreferrer noopener nofollow">https://www.npmjs.com/package/react-native-push-notification</a>”这个库。</p>

<p>我试过这段代码:- </p>

<pre><code>export function setupPushNotification(handleNotification) {
    PushNotification.configure({

    onRegister: function (token) {

      if (Platform.OS == &#39;ios&#39;) {
            firebase.messaging().getToken().then((token) =&gt; {
                alert(JSON.stringify(token))
                requestaddAuthToke(token)
            .then((data) =&gt; {
                console.log(&#34;hello2 &#34; + JSON.stringify(data))
            })
            .catch((error) =&gt; {
                console.log(&#34;hello3 &#34; + JSON.stringify(error.message));
            })
            });
      }
      else {
            console.log(&#34;hello2 &#34; + JSON.stringify(token.token))
            requestaddAuthToke(token.token)
            .then((data) =&gt; {
                console.log(&#34;hello2 &#34; + JSON.stringify(data))
            })
            .catch((error) =&gt; {
                console.log(&#34;hello3 &#34; + JSON.stringify(error.message));
            })
      }
    },

    onNotification: function (notification) {
      const clicked = notification.userInteraction;
      if (clicked)
            handleNotification(notification.episodeCode)

      if (Platform.OS == &#39;ios&#39;)
            notification.finish(PushNotificationIOS.FetchResult.NoData);

    },

    senderID: &#34;529815244511&#34;,
    permissions: {
      alert: true,
      badge: true,
      sound: true
    },



    popInitialNotification: true,
    requestPermissions: true,


})

return PushNotification
}
</code></pre>

<p>我们将不胜感激任何类型的帮助。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您同时使用 react-native-push-notification 和 react-native-one-signal。唯一的问题是仅在 iOS 中点击本地通知时未调用 onNotification。</p>
<p>寻求解决方案:</p>
<p> <a href="https://github.com/zo0r/react-native-push-notification/issues/919#issuecomment-439373380" rel="noreferrer noopener nofollow">https://github.com/zo0r/react-native-push-notification/issues/919#issuecomment-439373380</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于android - React Native 中的推送通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/57657528/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/57657528/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: android - React Native 中的推送通知