Example of Working with Push Notifications

To work with push notifications, create the PushActivity application, which uses the methods implemented in the PushAPI unit.

The application can be used to subscribe to messages with the specified topic and add a token to mobile platform server.

Before creating the PushActivity application set up the Firebase service and push notifications in the administrator console.

Set up the Firebase service

Set up push notifications

The PushActivity application includes one screen, text input area and buttons:

The text input area contains message topic name. The text input area is empty by default.

To view application work results, use the interactive example:

  1. Click the Topics button.

public class PushActivity extends AppCompatActivity {
   private static final String MY_URL = "http://testmasterfmp.fsight.cloud/";
   private static final VersionAPI MY_VERSION_API = VersionAPI.V_1;
   private static final String MY_ENVIRONMENT = "Leonid_environment";
   private static final String MY_PROJECT = "Leonid_project";
   private static final String MY_VERSION = "v1";
   private static final String MY_LOGIN = "Leonid";
   private static final String MY_PASSWORD = "123123";
   private static final String TAG = "FileActivity";
 
   private HyperHive hyperHive;
   private TextView textView;
   private EditText editText;
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_push);
 
       textView = findViewById(R.id.text_view_push);
       editText = findViewById(R.id.edit_text_push);
 
       Handler uiHandler = new Handler();
 
       hyperHive = new HyperHiveState(getApplicationContext())
               .setHostWithSchema(MY_URL)
               .setApiVersion(MY_VERSION_API)
               .setEnvironmentSlug(MY_ENVIRONMENT)
               .setProjectSlug(MY_PROJECT)
               .setVersionProject(MY_VERSION)
               .setHandler(uiHandler)
               .buildHyperHive();
 
       authentication();
   }
 
   public void authentication() {
       boolean status = hyperHive.authAPI.auth(MY_LOGIN, MY_PASSWORD, true).execute().isOk();
       showStatus(status);
   }
 
   // Get token by means of Firebase for mobile device
   public void getFirebaseToken(View view) {
       FirebaseInstanceId.getInstance().getInstanceId()
               .addOnCompleteListener(task -> {
                   if (!task.isSuccessful()) {
                       Log.w(TAG, "getInstanceId failed", task.getException());
                       return;
                   }
 
                   String token = task.getResult().getToken();
 
                   Log.d(TAG, token);
                   editText.setText(token);
               });
   }
 
   private void showStatus(boolean status) {
       Log.d(TAG, "status: " + status);
       Toast.makeText(this, String.valueOf(status), Toast.LENGTH_SHORT).show();
   }
 
   // Get message topics list
   public void getTopicsPush(View view) {
       String topics = hyperHive.pushAPI.getTopicsInRawString().execute();
       textView.setText(topics);
   }
 
   // Get tokens list
   public void getTokensPush(View view) {
       String tokens = hyperHive.pushAPI.getTokensInRawString().execute();
       textView.setText(tokens);
   }
  
   // Add a token
   public void addTokenPush(View view) {
       String token = editText.getText().toString();
       boolean status = hyperHive.pushAPI.addToken(token).execute().isOk();
       showStatus(status);
   }
 
   // Delete token
   public void removeTokenPush(View view) {
       String token = editText.getText().toString();
       boolean status = hyperHive.pushAPI.removeTokens(Arrays.asList(token)).execute().isOk();
       showStatus(status);
   }
 
   // Subscribe to receiving messages with the specified topic
   public void subscribePush(View view) {
       String topic = editText.getText().toString();
       boolean status = hyperHive.pushAPI.subscribe(Arrays.asList(topic)).execute().isOk();
       showStatus(status);
   }
 
   // Unsubscribe from receiving messages with the specified topic
   public void unsubscribePush(View view) {
       String topic = editText.getText().toString();
       boolean status = hyperHive.pushAPI.unsubscribe(Arrays.asList(topic)).execute().isOk();
       showStatus(status);
   }
}

Firebase service:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
   private static final String TAG = "MyFirebaseMsgService";
 
   @Override
   public void onMessageReceived(RemoteMessage remoteMessage) {
       Log.d(TAG, "From: " + remoteMessage.getFrom());
 
       if (remoteMessage.getData().size() > 0) {
           Log.d(TAG, "Message data payload: " + remoteMessage.getData());
       }
 
       if (remoteMessage.getNotification() != null) {
           Log.d(TAG, "title: " + remoteMessage.getNotification().getTitle());
           Log.d(TAG, "body: " + remoteMessage.getNotification().getBody());
       }
   }
 
   @Override
   public void onNewToken(String token) {
       Log.d(TAG, "Refreshed token: " + token);
   }
}

See also:

Examples of Android Framework Use