SDK

Authentication

This is an add-on feature of the Notificare library. You will need to subscribe to the Users & Authentication add-on and configure this service as described here. With this feature your application can provide creation, authentication and management of user accounts.

In this page we will dive deeper into this functionality. This functionality is built based on the OAuth2 standard, providing your application with the means to create accounts, authenticate users, send email activation, recover and change passwords and refresh access tokens when needed.

First, add two activities in your AndroidManifest.xml:

<activity
    android:name=".ResetPassActivity"
    android:label="@string/title_reset_pass" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="push.notifica.re" android:pathPrefix="/oauth/resetpassword/APP_ID_HERE" />
    </intent-filter>
</activity>

<activity
    android:name=".ValidateActivity"
    android:label="@string/title_validate" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="push.notifica.re" android:pathPrefix="/oauth/validate/APP_ID_HERE" />
    </intent-filter>
</activity>

These activities will handle the reset password and account activation process after users click those from an automated email message. Make sure you replace APP_ID_HERE with your own ID. You can find the ID in your app's configuration page, as described here.

Create an Account

You are able to create an account by simply invoking the method below and providing three required parameters: email, password and name.

Notificare.shared().createAccount(email, password, name, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Validate an Account

By default, a newly created account will receive a validation email. This will contain a link to validate the email address. If you've set up the Validate Activity as described above, the validation token can be parsed from the incoming data in the launch intent.

Notificare.shared().validateUser(token, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Recover an Account

Provide your users with a way of requesting a new password when they forget it. You will have to acquire an email address from your user and call the following method. Upon success, an email with instructions will be sent to your user.

Notificare.shared().sendPassword(email, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Reset a Password

Provide your users with a way of changing their password upon request. This method will required both a new password and the token received in the email. If you set up the reset password activity to handle the correct URL scheme, you can parse the token off the incoming data in the launch intent.

Notificare.shared().resetPassword(password, token, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Authenticate an Account

To authenticate the user invoke the following method:

Notificare.shared().userLogin(email, password, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

User Details

Once the user is signed in you can easily get their data by calling the following method. On success it will return the user object.

Notificare.shared().fetchUserDetails(new NotificareCallback<NotificareUser>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(NotificareUser user) {

    }
});

Access Token

At any time after they have signed in, you can generate a access token. This will also create a Push Mail address. Once you generate an access token, you can send push remote notifications to your users using an email message. Every time this method is invoked a new token/email will be generated.

Notificare.shared().generateAccessToken(new NotificareCallback<NotificareUser>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(NotificareUser user) {

    }
});

Change a Password

To provide your signed in users with the means to change their password you can easily invoke the following method.

Notificare.shared().changePassword(password, new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Sign out

To logout the user invoke the following method:

Notificare.shared().userLogout(new NotificareCallback<Boolean>(){
    @Override
    public void onError(NotificareError error) {

    }

    @Override
    public void onSuccess(Boolean success) {

    }
});

Handling Errors

All methods will implement a onError method that will provide you the server responses when an error occurs. You can then react to those errors to change your UI accordingly or move between activities.

@Override
public void onError(NotificareError error) {
    switch(error.getCode()){
      case NotificareError.FORBIDDEN:
      case NotificareError.UNAUTHORIZED:
      case NotificareError.CONFLICT:
      case NotificareError.UNKNOWN:
        Log.d("NotificareError", error.getMessage());
        break;
      default:
        Log.d("NotificareError", error.getMessage());
        break;
    }
}