Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. package ag.fth.akn.my.receiver;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6.  
  7. import com.google.gson.JsonObject;
  8. import com.google.gson.JsonParser;
  9. import com.pushwoosh.Pushwoosh;
  10. import com.pushwoosh.notification.NotificationServiceExtension;
  11. import com.pushwoosh.notification.PushMessage;
  12. import com.pushwoosh.notification.PushwooshNotificationFactory;
  13.  
  14. import org.json.JSONObject;
  15.  
  16. import ag.fth.akn.my.InvoiceOverviewActivity;
  17. import ag.fth.akn.my.usecase.AppContentDeleter;
  18.  
  19. public class PushwooshNotificationReceiver extends NotificationServiceExtension {
  20.  
  21.     public PushwooshNotificationReceiver() {
  22.         super();
  23.     }
  24.  
  25.     @Override
  26.     protected void startActivityForPushMessage(PushMessage pushMessage) {
  27.  
  28.         String data = pushMessage.getCustomData();
  29.         JsonObject jsonObject = new JsonObject();
  30.         if(data != null) {
  31.             jsonObject = (new JsonParser()).parse(data).getAsJsonObject();
  32.         }
  33.  
  34.         if(jsonObject.has("url")) {
  35.             openUrlInBrowser(jsonObject.get("url").getAsString());
  36.             return;
  37.         }
  38.  
  39.         if(jsonObject.has("activity")) {
  40.             startActivity(jsonObject, pushMessage);
  41.             return;
  42.         }
  43.  
  44.         super.startActivityForPushMessage(pushMessage);
  45.     }
  46.  
  47.     private void startActivity(JsonObject jsonObject, PushMessage pushMessage) {
  48.         String activity = jsonObject.get("activity").getAsString();
  49.  
  50.         switch (activity.toLowerCase()) {
  51.             case "invoice":
  52.                 // start your activity instead:
  53.                 Intent launchIntent  = new Intent(getApplicationContext(), InvoiceOverviewActivity.class);
  54.                 launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
  55.  
  56.                 // (Optional) pass notification data to Activity
  57.                 launchIntent.putExtra(Pushwoosh.PUSH_RECEIVE_EVENT, pushMessage.toJson().toString());
  58.                 launchIntent.putExtra("order_id", jsonObject.get("order_id").getAsString());
  59.  
  60.                 getApplicationContext().startActivity(launchIntent);
  61.                 break;
  62.             default:
  63.                 super.startActivityForPushMessage(pushMessage);
  64.                 return;
  65.         }
  66.     }
  67.  
  68.     private void openUrlInBrowser(String url) {
  69.         Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  70.         urlIntent.setFlags(urlIntent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
  71.  
  72.         Context context = getApplicationContext();
  73.         if (context != null) {
  74.             context.startActivity(urlIntent);
  75.         }
  76.     }
  77.  
  78.     @Override
  79.     public boolean onMessageReceived(PushMessage pushMessage) {
  80.         String data = pushMessage.getCustomData();
  81.         JsonObject jsonObject = new JsonObject();
  82.         if(data != null) {
  83.             jsonObject = (new JsonParser()).parse(data).getAsJsonObject();
  84.         }
  85.  
  86.         if(jsonObject.has("action") && jsonObject.get("action").getAsString().equals("delete_all_data")) {
  87.             AppContentDeleter deleteUseCase = new AppContentDeleter();
  88.             deleteUseCase.deleteAll();
  89.             return true;
  90.         }
  91.  
  92.         return false;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement