菜鸟教程小白 发表于 2022-12-12 12:48:17

ios:当应用程序处于焦点时禁用警报通知(OneSignal)


                                            <p><p>我使用 OneSignal sdk 进行 nativereact 。当应用程序处于焦点时,我尝试禁用内部警报。你知道怎么做吗? </p>

<p>这是我的代码:</p>

<pre><code>OneSignal.configure({
onIdsAvailable: function (device) {
console.log(&#39;UserId = &#39;, device.userId);
console.log(&#39;PushToken = &#39;, device.pushToken);
getUserId(device.userId);
},
onNotificationReceived: function (notification) {
OneSignal.checkPermissions((permissions) =&gt; {
console.log(permissions);
});
OneSignal.inFocusDisplaying(0);
},
onNotificationOpened: function(message, data, isActive) {
console.log(&#34;MS TYPE&#34;, message);
switch(message.notification.payload.additionalData.type) {
case &#39;1&#39;:
       Actions.messenger({id:   message.notification.payload.additionalData.userID});
    break;
case &#39;2&#39;:
      Actions.overview();
    break;
}
}
});
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为您代码中的问题在于您放置 <code>OneSignal.inFocusDisplaying(0);</code></p>

<p>你需要把它放在<code>componentWillMount</code>之类的</p>

<pre><code>componentWillMount () {
    OneSignal.addEventListener(&#39;received&#39;, this.onReceived.bind(this))
    OneSignal.addEventListener(&#39;opened&#39;, this.onOpened)
    OneSignal.addEventListener(&#39;registered&#39;, this.onRegistered)
    OneSignal.addEventListener(&#39;ids&#39;, this.onIds.bind(this))

    // Set inFocusDisplaying to prevent the default push notification alert when the is in focus
    // Android ONLY
    OneSignal.inFocusDisplaying(0)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios:当应用程序处于焦点时禁用警报通知(OneSignal),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41977942/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41977942/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios:当应用程序处于焦点时禁用警报通知(OneSignal)