Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ag.fth.akn.my.receiver;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import com.pushwoosh.Pushwoosh;
- import com.pushwoosh.notification.NotificationServiceExtension;
- import com.pushwoosh.notification.PushMessage;
- import com.pushwoosh.notification.PushwooshNotificationFactory;
- import org.json.JSONObject;
- import ag.fth.akn.my.InvoiceOverviewActivity;
- import ag.fth.akn.my.usecase.AppContentDeleter;
- public class PushwooshNotificationReceiver extends NotificationServiceExtension {
- public PushwooshNotificationReceiver() {
- super();
- }
- @Override
- protected void startActivityForPushMessage(PushMessage pushMessage) {
- String data = pushMessage.getCustomData();
- JsonObject jsonObject = new JsonObject();
- if(data != null) {
- jsonObject = (new JsonParser()).parse(data).getAsJsonObject();
- }
- if(jsonObject.has("url")) {
- openUrlInBrowser(jsonObject.get("url").getAsString());
- return;
- }
- if(jsonObject.has("activity")) {
- startActivity(jsonObject, pushMessage);
- return;
- }
- super.startActivityForPushMessage(pushMessage);
- }
- private void startActivity(JsonObject jsonObject, PushMessage pushMessage) {
- String activity = jsonObject.get("activity").getAsString();
- switch (activity.toLowerCase()) {
- case "invoice":
- // start your activity instead:
- Intent launchIntent = new Intent(getApplicationContext(), InvoiceOverviewActivity.class);
- launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
- // (Optional) pass notification data to Activity
- launchIntent.putExtra(Pushwoosh.PUSH_RECEIVE_EVENT, pushMessage.toJson().toString());
- launchIntent.putExtra("order_id", jsonObject.get("order_id").getAsString());
- getApplicationContext().startActivity(launchIntent);
- break;
- default:
- super.startActivityForPushMessage(pushMessage);
- return;
- }
- }
- private void openUrlInBrowser(String url) {
- Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
- urlIntent.setFlags(urlIntent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
- Context context = getApplicationContext();
- if (context != null) {
- context.startActivity(urlIntent);
- }
- }
- @Override
- public boolean onMessageReceived(PushMessage pushMessage) {
- String data = pushMessage.getCustomData();
- JsonObject jsonObject = new JsonObject();
- if(data != null) {
- jsonObject = (new JsonParser()).parse(data).getAsJsonObject();
- }
- if(jsonObject.has("action") && jsonObject.get("action").getAsString().equals("delete_all_data")) {
- AppContentDeleter deleteUseCase = new AppContentDeleter();
- deleteUseCase.deleteAll();
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement