如何防止收到 OneSignal 通知的警报?

How to prevent alert with incoming OneSignal notification?

我使用 OneSignal 推送通知。当 android 应用程序在前台并收到通知时,它会创建一个带有通知的警告框。如何防止收到通知时出现这种情况?

来自 SDK documentation - 当您启动 Init OneSignal 时,请确保使用“None”调用 inFocusDisplaying 以在应用程序 AlertBox 中禁用 OneSignal。

也在 NotificationReceivedHandler 部分 -

Important behavior notes - If you will be displaying your own in app message when a notification is received make sure to call inFocusDisplaying with None to disable OneSignal's in app AlertBox.

我遇到了类似的问题,我使用 inFocusDisplaying

解决了它

下面是如何在 android 中使用它。

    public class MyApplicationClass extends Application {

private static Context context;
PlayerIdsession session;

public static Context getContext() {
    return context;
}

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();
    //MyNotificationOpenedHandler : This will be called when a notification is tapped on.
    //MyNotificationReceivedHandler : This will be called when a notification is received while your app is running.
    OneSignal.startInit(this)
            .setNotificationOpenedHandler(new MyNotiOpenedHandler())
            .setNotificationReceivedHandler( new MyNotiReceivedHandler() )
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .init();

    OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
        @Override
        public void idsAvailable(String userId, String registrationId) {

            if (userId != null){
                session=new PlayerIdsession(context);
                session.savePlayerId(userId);
                Log.d("debug", "PlayerId:" + userId);
            }

           /* if (registrationId != null){
                Log.d("debug", "registrationId:" + registrationId);
        }*/

        }
    });
}
}

使用此代码行,我解决了我的问题。

OneSignal.inFocusDisplaying(2);

只需将此行添加到您的 windows.plugin.signal

.inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.Notification)

例如:-

window.plugins.OneSignal
.startInit("YOUR_APPID")
.inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.Notification)
.endInit();

它在 OneSignal 4.0 中有所改变。

对于 Kotlin:

OneSignal.setNotificationWillShowInForegroundHandler { notificationReceivedEvent ->
    notificationReceivedEvent.complete(null)
}

对于Java:

OneSignal.setNotificationWillShowInForegroundHandler(new NotificationWillShowInForegroundHandler() {
  @Override
  void notificationWillShowInForeground(OSNotificationReceivedEvent notificationReceivedEvent) {    
     notificationReceivedEvent.complete(null);
  }
});