React-native-notifications: Android: Customising clearing of notifications.

Created on 23 Oct 2017  路  5Comments  路  Source: wix/react-native-notifications

Today I was trying to achieve desired behaviour of not discarding all notifications from drawer if I open the app - regardless if from dead state or background.

I ended up writing a facade for your package, which was overriding onAppVisible() and initialize() functions, to avoid calling clearAll().

Do you think it would be good idea to add some sort of config to allow this to be done without using a facade for the package?

馃彋 stale

Most helpful comment

Thanks @Lastin for creating this issue. Like @kaloncheung124, it is very annoying to see all the notifications disappear after opening only one. Actually, I do not understand this default behaviour.

I succeeded to disable the _clearAll_ method of the _PushNotificationDrawer_ class (responsible of the "notifications clearance") by :

Step 1: create a new class that extends _PushNotificationsDrawer_. Here is an example:

/**
 * IMPORTANT: we do that only to disable the {@link PushNotificationsDrawer#clearAll()} method.
 */
public class CustomPushNotificationsDrawer extends PushNotificationsDrawer {
    /** Tag for logs. */
    private static final String TAG = CustomPushNotificationsDrawer.class.getSimpleName();

    public static IPushNotificationsDrawer get(Context context) {
        return CustomPushNotificationsDrawer.get(context, new AppLaunchHelper());
    }

    public static IPushNotificationsDrawer get(Context context, AppLaunchHelper appLaunchHelper) {
        final Context appContext = context.getApplicationContext();
        if (appContext instanceof INotificationsDrawerApplication) {
            return ((INotificationsDrawerApplication) appContext)
                    .getPushNotificationsDrawer(context, appLaunchHelper);
        }

        return new CustomPushNotificationsDrawer(context, appLaunchHelper);
    }

    /** {@inheritDoc} */
    public CustomPushNotificationsDrawer(Context context, AppLaunchHelper appLaunchHelper) {
        super(context, appLaunchHelper);
    }

    @Override
    protected void clearAll() {
        Log.d(TAG, "React-Native, the best library ever to build great apps. I'm joking of course :)");
        // Disable this method!
    }
}

Step 2: create a new class that extends _PushNotification_. Here is an example:
````
/**

/** {@link com.wix.reactnativenotifications.core.NotificationIntentAdapter#PUSH_NOTIFICATION_EXTRA_NAME} */
private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";

/** {@inheritDoc} */
public CustomPushNotification(Context context, Bundle bundle,
                              AppLifecycleFacade appLifecycleFacade,
                              AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) {
    super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper);
}

/**
 * https://github.com/wix/react-native-notifications/issues/126
 */
@Override
public void onOpened() {
    digestNotification();
}

/**
 * https://github.com/wix/react-native-notifications/issues/126
 */
@Override
protected PendingIntent getCTAPendingIntent() {
    final Intent intent = new Intent(mContext, ProxyService.class);
    intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, mNotificationProps.asBundle());
    intent.setAction(Long.toString(System.currentTimeMillis()));
    return PendingIntent.getService(mContext, PENDING_INTENT_CODE, intent,
            PendingIntent.FLAG_ONE_SHOT);
}

}
````

Step 3: extend the _Application_ class (if it is not already the case) and implement the interfaces _INotificationsApplication_ and _INotificationsDrawerApplication_. Here is an example:
````
/**

  • https://github.com/wix/react-native-notifications/wiki/Android:-working-with-RNN
    */
    public class MainApplication extends NavigationApplication implements INotificationsApplication, INotificationsDrawerApplication {
    ...
    @Override
    public IPushNotification getPushNotification(Context context, Bundle bundle,
    AppLifecycleFacade defaultFacade,
    AppLaunchHelper defaultAppLaunchHelper) {
    //region https://github.com/wix/react-native-notifications/issues/69
    if (bundle != null) {
    bundle.putString("body", bundle.getString("message"));
    }
    //endregion
    return new CustomPushNotification(
    context,
    bundle,
    notificationsLifecycleFacade, // Instead of defaultFacade!!!
    defaultAppLaunchHelper,
    new JsIOHelper()
    );
    }
@Override
public IPushNotificationsDrawer getPushNotificationsDrawer(
        Context context,
        AppLaunchHelper defaultAppLaunchHelper) {
    return new CustomPushNotificationsDrawer(context, defaultAppLaunchHelper);
}

}
````

It works for me but since I'm not a React-Native developer (and as you probably see, I do not like so much this framework) I cannot be sure that it is the best solution for what we want to do.

All 5 comments

I would also be interested in this. It's very annoying to have all the notifications cleared on every open of the app.

Thanks @Lastin for creating this issue. Like @kaloncheung124, it is very annoying to see all the notifications disappear after opening only one. Actually, I do not understand this default behaviour.

I succeeded to disable the _clearAll_ method of the _PushNotificationDrawer_ class (responsible of the "notifications clearance") by :

Step 1: create a new class that extends _PushNotificationsDrawer_. Here is an example:

/**
 * IMPORTANT: we do that only to disable the {@link PushNotificationsDrawer#clearAll()} method.
 */
public class CustomPushNotificationsDrawer extends PushNotificationsDrawer {
    /** Tag for logs. */
    private static final String TAG = CustomPushNotificationsDrawer.class.getSimpleName();

    public static IPushNotificationsDrawer get(Context context) {
        return CustomPushNotificationsDrawer.get(context, new AppLaunchHelper());
    }

    public static IPushNotificationsDrawer get(Context context, AppLaunchHelper appLaunchHelper) {
        final Context appContext = context.getApplicationContext();
        if (appContext instanceof INotificationsDrawerApplication) {
            return ((INotificationsDrawerApplication) appContext)
                    .getPushNotificationsDrawer(context, appLaunchHelper);
        }

        return new CustomPushNotificationsDrawer(context, appLaunchHelper);
    }

    /** {@inheritDoc} */
    public CustomPushNotificationsDrawer(Context context, AppLaunchHelper appLaunchHelper) {
        super(context, appLaunchHelper);
    }

    @Override
    protected void clearAll() {
        Log.d(TAG, "React-Native, the best library ever to build great apps. I'm joking of course :)");
        // Disable this method!
    }
}

Step 2: create a new class that extends _PushNotification_. Here is an example:
````
/**

/** {@link com.wix.reactnativenotifications.core.NotificationIntentAdapter#PUSH_NOTIFICATION_EXTRA_NAME} */
private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";

/** {@inheritDoc} */
public CustomPushNotification(Context context, Bundle bundle,
                              AppLifecycleFacade appLifecycleFacade,
                              AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) {
    super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper);
}

/**
 * https://github.com/wix/react-native-notifications/issues/126
 */
@Override
public void onOpened() {
    digestNotification();
}

/**
 * https://github.com/wix/react-native-notifications/issues/126
 */
@Override
protected PendingIntent getCTAPendingIntent() {
    final Intent intent = new Intent(mContext, ProxyService.class);
    intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, mNotificationProps.asBundle());
    intent.setAction(Long.toString(System.currentTimeMillis()));
    return PendingIntent.getService(mContext, PENDING_INTENT_CODE, intent,
            PendingIntent.FLAG_ONE_SHOT);
}

}
````

Step 3: extend the _Application_ class (if it is not already the case) and implement the interfaces _INotificationsApplication_ and _INotificationsDrawerApplication_. Here is an example:
````
/**

  • https://github.com/wix/react-native-notifications/wiki/Android:-working-with-RNN
    */
    public class MainApplication extends NavigationApplication implements INotificationsApplication, INotificationsDrawerApplication {
    ...
    @Override
    public IPushNotification getPushNotification(Context context, Bundle bundle,
    AppLifecycleFacade defaultFacade,
    AppLaunchHelper defaultAppLaunchHelper) {
    //region https://github.com/wix/react-native-notifications/issues/69
    if (bundle != null) {
    bundle.putString("body", bundle.getString("message"));
    }
    //endregion
    return new CustomPushNotification(
    context,
    bundle,
    notificationsLifecycleFacade, // Instead of defaultFacade!!!
    defaultAppLaunchHelper,
    new JsIOHelper()
    );
    }
@Override
public IPushNotificationsDrawer getPushNotificationsDrawer(
        Context context,
        AppLaunchHelper defaultAppLaunchHelper) {
    return new CustomPushNotificationsDrawer(context, defaultAppLaunchHelper);
}

}
````

It works for me but since I'm not a React-Native developer (and as you probably see, I do not like so much this framework) I cannot be sure that it is the best solution for what we want to do.

Shouldn't it become a feature???

+1

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

The issue has been closed for inactivity.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

WhereBeTheDan picture WhereBeTheDan  路  5Comments

Bilal-Abdeen picture Bilal-Abdeen  路  4Comments

Mimble-Wimble picture Mimble-Wimble  路  3Comments

kaloncheung124 picture kaloncheung124  路  4Comments

daominhsangvn picture daominhsangvn  路  6Comments