0

I'm developing and application that use OneSignal API to receive push messages. Below the code that handle open messages:

Receiver

public class OneSignalBroadcastReceiver extends BroadcastReceiver implements OneSignal.NotificationOpenedHandler {


    private static RecadosDataSource rec;
    private static Context mcontext;
    private static Boolean opened = false;
    private static String Titulo = "";
    private static String Mensagem = "";


    @Override
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
        try {

            this.mcontext = MainActivity.getInstance().getApplication().getApplicationContext();
            rec = new RecadosDataSource(this.mcontext);
            databaseManager(true,this.mcontext);
            rec.updateRecados(additionalData.getString("title"), message);
            databaseManager(false, this.mcontext);

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

}

The problem is How get a context in this class? In the current way I can't get the context.

Juliano Oliveira
  • 868
  • 1
  • 9
  • 29
  • 1
    You get a `Context` passed to you in `onReceive()`. That method should be required for a `BroadcastReceiver` to be of much use. That being said, I know nothing of OneSignal, let alone its API. – CommonsWare Apr 06 '16 at 13:30
  • I have a override of the onReceive(), but the first point is the notificationOpened. – Juliano Oliveira Apr 06 '16 at 13:38
  • It would be very bizarre if `notificationOpened()` were called before `onReceive()`. If that is the case, contact OneSignal and ask them for advice. The entry point of a `BroadcastReceiver` is supposed to be `onReceive()` -- if they are breaking that, it is their responsibility to tell developers how to handle issues like yours. – CommonsWare Apr 06 '16 at 13:41

2 Answers2

0

this is just addition CommonsWare comment. Do you call super when override onReceive? if yes try to put your code before it

Community
  • 1
  • 1
Alex
  • 130
  • 11
0

The NotificationOpenedHandler interface is a simple callback rather than an Android Broadcast. You can initialize OneSignal with any Context and set a notification opened handler anytime in your app with the following code:

OneSignal.startInit(context).setNotificationOpenedHandler(new YourNotificationOpenedHandler()).init();

We however recommend calling this from the onCreate of your Application class as this allows for the most flexibility. A more complete example is shown in step 4. Add Optional NotificationOpenedHandler of our SDK setup guide.

jkasten
  • 3,899
  • 3
  • 32
  • 51