Android - OneSignal回调方法永远不会被触发(notificationReceived - notificationOpened)

时间:2016-11-02 16:26:39

标签: android callback onesignal

我正在使用信号来推送通知,但它工作正常但我遇到了“接收通知”的问题,因为我想在收到通知时采取行动。

这是我的代码,我按照文档但我什么也没得到。问题出在哪里?

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!!!!!!");

}
}
}

2 个答案:

答案 0 :(得分:6)

NotificationReceivedHandler只有在您的应用位于前台时才会触发。

如果您需要对收到的通知采取措施,无论您的应用状态如何,您都需要设置OneSingal NotificationExtenderService

答案 1 :(得分:0)

直接从文档中引用

  

默认情况下,当点击通知时,OneSignal将打开或恢复您的启动器活动。您可以通过在AndroidManifest.xml中的应用程序标记内添加元数据标记com.onesignal.NotificationOpened.DEFAULT设置为DISABLE来禁用此行为

see for yourself