Android Correct Usage Onesignal

19-08-2017

When using onesignal as notification system in Android application, we should insert following codes in ApplicationManifest.xml file:

 <meta-data
            android:name="com.onesignal.NotificationOpened.DEFAULT"
            android:value="DISABLE" />

After adding this meta-data, Android will disable launcher activity to be opened.

Handle notification opened event as follows:

public class App extends Application {
    protected boolean active = true;
    protected int splashTime = 3000;

    @Override
    public void onCreate() {

        super.onCreate();
        MultiDex.install(getApplicationContext());

        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
            @Override
            public void notificationOpened(OSNotificationOpenResult result) {
                String launchURL = null;
                try {
                    if (result.notification.payload.additionalData == null ||
                            result.notification.payload.additionalData.get("CATEGORYID") == null)
                        return;
                    Constant.CATEGORYID = Integer.valueOf(result.notification.payload.additionalData.get("CATEGORYID").toString());
                    Constant.PROJECTID = result.notification.payload.additionalData.get("PROJECTID").toString();
                    Intent intent = new Intent(getApplicationContext(), CategoryList_Activity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    //Log.e("item", ""+Constant.CATEGORY_ID);
                    startActivity(intent);

                } catch (JSONException e) {
                    Intent intent = new Intent(getApplicationContext(), Splash_Activity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        }).init();

    }
}

© 2019 All rights reserved. Codesenior.COM