Guest User

Untitled

a guest
May 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. package com.adidas.glitch;
  2.  
  3. import android.app.ActivityManager;
  4. import android.app.ActivityManager.RunningAppProcessInfo;
  5. import android.app.Application;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Looper;
  9. import android.util.Log;
  10.  
  11. import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
  12. import com.dieam.reactnativepushnotification.modules.RNPushNotificationHelper;
  13. import com.adidas.glitch.MyPushNotificationJSDelivery;
  14. import com.facebook.react.ReactApplication;
  15. import com.facebook.react.ReactInstanceManager;
  16. import com.facebook.react.bridge.ReactApplicationContext;
  17. import com.facebook.react.bridge.ReactContext;
  18. import com.google.android.gms.gcm.GcmListenerService;
  19.  
  20. import org.json.JSONObject;
  21.  
  22. import java.util.List;
  23. import java.util.Random;
  24.  
  25. import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
  26.  
  27. public class MyListenerService extends GcmListenerService {
  28.  
  29. @Override
  30. public void onMessageReceived(String from, final Bundle bundle) {
  31. Log.d("MYListener", "onMessageReceived");
  32. JSONObject data = getPushData(bundle.getString("data"));
  33. if (data != null) {
  34. if (!bundle.containsKey("message")) {
  35. bundle.putString("message", data.optString("alert", "Notification received"));
  36. }
  37. if (!bundle.containsKey("title")) {
  38. bundle.putString("title", data.optString("title", null));
  39. }
  40. if (!bundle.containsKey("sound")) {
  41. bundle.putString("soundName", data.optString("sound", null));
  42. }
  43. if (!bundle.containsKey("color")) {
  44. bundle.putString("color", data.optString("color", null));
  45. }
  46.  
  47. final int badge = data.optInt("badge", -1);
  48. if (badge >= 0) {
  49. ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(this, badge);
  50. }
  51. }
  52.  
  53. Log.v(LOG_TAG, "onMessageReceived: " + bundle);
  54.  
  55. // We need to run this on the main thread, as the React code assumes that is true.
  56. // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
  57. // "Can't create handler inside thread that has not called Looper.prepare()"
  58. Handler handler = new Handler(Looper.getMainLooper());
  59. handler.post(new Runnable() {
  60. public void run() {
  61. // Construct and load our normal React JS code bundle
  62. ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
  63. ReactContext context = mReactInstanceManager.getCurrentReactContext();
  64. // If it's constructed, send a notification
  65. if (context != null) {
  66. handleRemotePushNotification((ReactApplicationContext) context, bundle);
  67. } else {
  68. // Otherwise wait for construction, then send the notification
  69. mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
  70. public void onReactContextInitialized(ReactContext context) {
  71. handleRemotePushNotification((ReactApplicationContext) context, bundle);
  72. }
  73. });
  74. if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
  75. // Construct it in the background
  76. mReactInstanceManager.createReactContextInBackground();
  77. }
  78. }
  79. }
  80. });
  81. }
  82.  
  83. private JSONObject getPushData(String dataString) {
  84. try {
  85. return new JSONObject(dataString);
  86. } catch (Exception e) {
  87. return null;
  88. }
  89. }
  90.  
  91. private void handleRemotePushNotification(ReactApplicationContext context, Bundle bundle) {
  92.  
  93. // If notification ID is not provided by the user for push notification, generate one at random
  94. if (bundle.getString("id") == null) {
  95. Random randomNumberGenerator = new Random(System.currentTimeMillis());
  96. bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
  97. }
  98.  
  99. Boolean isForeground = isApplicationInForeground();
  100.  
  101. if (isForeground) {
  102. MyPushNotificationJSDelivery jsDelivery = new MyPushNotificationJSDelivery(context);
  103. bundle.putBoolean("foreground", isForeground);
  104. bundle.putBoolean("userInteraction", false);
  105. jsDelivery.notifyNotification(bundle);
  106. // If contentAvailable is set to true, then send out a remote fetch event
  107. if (bundle.getString("contentAvailable", "false").equalsIgnoreCase("true")) {
  108. jsDelivery.notifyRemoteFetch(bundle);
  109. }
  110. }
  111.  
  112. Log.v(LOG_TAG, "sendNotification: " + bundle);
  113.  
  114. if (!isForeground) {
  115. Application applicationContext = (Application) context.getApplicationContext();
  116. RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);
  117. if (!bundle.containsKey("com.urbanairship.push.ALERT")) {
  118. pushNotificationHelper.sendToNotificationCentre(bundle);
  119. }
  120. }
  121. }
  122.  
  123. private boolean isApplicationInForeground() {
  124. ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
  125. List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
  126. for (RunningAppProcessInfo processInfo : processInfos) {
  127. if (processInfo.processName.equals(getApplication().getPackageName())) {
  128. if (processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
  129. for (String d : processInfo.pkgList) {
  130. return true;
  131. }
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. }
Add Comment
Please, Sign In to add comment