Android - OneSignal 回调方法从未触发(notificationReceived - notificationOpened)

Android - OneSignal callback methods never fired(notificationReceived - notificationOpened)

我正在使用 onesignal 推送通知,它工作正常,但我遇到了 "Receiving Notifications" 的问题,因为我想在收到通知时采取行动。

这是我的代码,我按照文档进行操作,但一无所获。问题出在哪里?

public class MyApplication extends Application {
static Context context;
@Override public void onCreate() { 
  super.onCreate(); 
  context = getApplicationContext();


OneSignal.startInit(this) 
 .autoPromptLocation(true) 
 .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())   
 .setNotificationReceivedHandler(new ExampleNotificationReceivedHandler())
 .init(); 
}

private class ExampleNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {

@Override
public void notificationReceived(OSNotification notification) {
    Log.d("OneSignalExample", "notificationReceived!!!!!!");

}
}

  private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

@Override
public void notificationOpened(OSNotificationOpenResult openedResult) {
    Log.d("OneSignalExample", "notificationOpened!!!!!!");

}
}
}

直接引用文档

By default OneSignal will open or resume your launcher Activity when a notification is tapped on. You can disable this behavior by adding the meta-data tag com.onesignal.NotificationOpened.DEFAULT set to DISABLE inside your application tag in your AndroidManifest.xml

see for yourself

NotificationReceivedHandler 仅当您的应用程序在前台时才会触发。

如果您需要对收到的通知采取行动,而不管您的应用程序处于什么状态,您将需要设置一个 OneSingal NotificationExtenderService