|
| 1 | +package io.fullstack.firestack; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.content.IntentFilter; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.BroadcastReceiver; |
| 9 | +import android.util.Log; |
| 10 | + |
| 11 | +import com.facebook.react.bridge.Arguments; |
| 12 | +import com.facebook.react.bridge.Callback; |
| 13 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 14 | +import com.facebook.react.bridge.ReactContext; |
| 15 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 16 | +import com.facebook.react.bridge.Promise; |
| 17 | +import com.facebook.react.bridge.ReactMethod; |
| 18 | +import com.facebook.react.bridge.ReadableMap; |
| 19 | +import com.facebook.react.bridge.ReadableMapKeySetIterator; |
| 20 | +import com.facebook.react.bridge.ReadableType; |
| 21 | +import com.facebook.react.bridge.WritableMap; |
| 22 | + |
| 23 | +import com.google.firebase.iid.FirebaseInstanceId; |
| 24 | +import com.google.firebase.messaging.FirebaseMessaging; |
| 25 | +import com.google.firebase.messaging.RemoteMessage; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by nori on 2016/09/12. |
| 29 | + */ |
| 30 | +public class FirestackCloudMessaging extends ReactContextBaseJavaModule { |
| 31 | + |
| 32 | + private static final String TAG = "FirestackCloudMessaging"; |
| 33 | + private static final String EVENT_NAME_TOKEN = "FirestackRefreshToken"; |
| 34 | + private static final String EVENT_NAME_NOTIFICATION = "FirestackReceiveNotification"; |
| 35 | + private static final String EVENT_NAME_SEND = "FirestackUpstreamSend"; |
| 36 | + |
| 37 | + public static final String INTENT_NAME_TOKEN = "io.fullstack.firestack.refreshToken"; |
| 38 | + public static final String INTENT_NAME_NOTIFICATION = "io.fullstack.firestack.ReceiveNotification"; |
| 39 | + public static final String INTENT_NAME_SEND = "io.fullstack.firestack.Upstream"; |
| 40 | + |
| 41 | + private ReactContext mReactContext; |
| 42 | + private IntentFilter mRefreshTokenIntentFilter; |
| 43 | + private IntentFilter mReceiveNotificationIntentFilter; |
| 44 | + private IntentFilter mReceiveSendIntentFilter; |
| 45 | + |
| 46 | + public FirestackCloudMessaging(ReactApplicationContext reactContext) { |
| 47 | + super(reactContext); |
| 48 | + mReactContext = reactContext; |
| 49 | + mRefreshTokenIntentFilter = new IntentFilter(INTENT_NAME_TOKEN); |
| 50 | + mReceiveNotificationIntentFilter = new IntentFilter(INTENT_NAME_NOTIFICATION); |
| 51 | + mReceiveSendIntentFilter = new IntentFilter(INTENT_NAME_SEND); |
| 52 | + initRefreshTokenHandler(); |
| 53 | + initMessageHandler(); |
| 54 | + initSendHandler(); |
| 55 | + Log.d(TAG, "New instance"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getName() { |
| 60 | + return TAG; |
| 61 | + } |
| 62 | + |
| 63 | + @ReactMethod |
| 64 | + public void getToken(final Callback callback) { |
| 65 | + |
| 66 | + try { |
| 67 | + String token = FirebaseInstanceId.getInstance().getToken(); |
| 68 | + Log.d(TAG, "Firebase token: " + token); |
| 69 | + callback.invoke(null, token); |
| 70 | + } catch (Exception e) { |
| 71 | + WritableMap error = Arguments.createMap(); |
| 72 | + error.putString("message", e.getMessage()); |
| 73 | + callback.invoke(error); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * |
| 79 | + */ |
| 80 | + private void initRefreshTokenHandler() { |
| 81 | + getReactApplicationContext().registerReceiver(new BroadcastReceiver() { |
| 82 | + @Override |
| 83 | + public void onReceive(Context context, Intent intent) { |
| 84 | + WritableMap params = Arguments.createMap(); |
| 85 | + params.putString("token", intent.getStringExtra("token")); |
| 86 | + FirestackUtils.sendEvent(mReactContext, EVENT_NAME_TOKEN, params); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + ; |
| 91 | + }, mRefreshTokenIntentFilter); |
| 92 | + } |
| 93 | + |
| 94 | + @ReactMethod |
| 95 | + public void subscribeToTopic(String topic, final Callback callback) { |
| 96 | + try { |
| 97 | + FirebaseMessaging.getInstance().subscribeToTopic(topic); |
| 98 | + callback.invoke(null,topic); |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + Log.d(TAG, "Firebase token: " + e); |
| 102 | + WritableMap error = Arguments.createMap(); |
| 103 | + error.putString("message", e.getMessage()); |
| 104 | + callback.invoke(error); |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @ReactMethod |
| 110 | + public void unsubscribeFromTopic(String topic, final Callback callback) { |
| 111 | + try { |
| 112 | + FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); |
| 113 | + callback.invoke(null,topic); |
| 114 | + } catch (Exception e) { |
| 115 | + WritableMap error = Arguments.createMap(); |
| 116 | + error.putString("message", e.getMessage()); |
| 117 | + callback.invoke(error); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private void initMessageHandler() { |
| 122 | + getReactApplicationContext().registerReceiver(new BroadcastReceiver() { |
| 123 | + @Override |
| 124 | + public void onReceive(Context context, Intent intent) { |
| 125 | + RemoteMessage remoteMessage = intent.getParcelableExtra("data"); |
| 126 | + Log.d(TAG, "Firebase onReceive: " + remoteMessage); |
| 127 | + WritableMap params = Arguments.createMap(); |
| 128 | + if (remoteMessage.getData().size() != 0) { |
| 129 | + WritableMap dataMap = Arguments.createMap(); |
| 130 | + Map<String, String> data = remoteMessage.getData(); |
| 131 | + //Set<String> keysIterator = data.keySet(); |
| 132 | + for (String key : data.keySet()) { |
| 133 | + dataMap.putString(key, data.get(key)); |
| 134 | + } |
| 135 | + params.putMap("data", dataMap); |
| 136 | + } else { |
| 137 | + params.putNull("data"); |
| 138 | + } |
| 139 | + if (remoteMessage.getNotification() != null) { |
| 140 | + WritableMap notificationMap = Arguments.createMap(); |
| 141 | + RemoteMessage.Notification notification = remoteMessage.getNotification(); |
| 142 | + notificationMap.putString("title", notification.getTitle()); |
| 143 | + notificationMap.putString("body", notification.getBody()); |
| 144 | + notificationMap.putString("icon", notification.getIcon()); |
| 145 | + notificationMap.putString("sound", notification.getSound()); |
| 146 | + notificationMap.putString("tag", notification.getTag()); |
| 147 | + params.putMap("notification", notificationMap); |
| 148 | + } else { |
| 149 | + params.putNull("notification"); |
| 150 | + } |
| 151 | + FirestackUtils.sendEvent(mReactContext, EVENT_NAME_NOTIFICATION, params); |
| 152 | + } |
| 153 | + }, mReceiveNotificationIntentFilter); |
| 154 | + } |
| 155 | + |
| 156 | + @ReactMethod |
| 157 | + public void send(String senderId, Integer messageId, String messageType, ReadableMap params) { |
| 158 | + FirebaseMessaging fm = FirebaseMessaging.getInstance(); |
| 159 | + RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId); |
| 160 | + remoteMessage.setMessageId(messageId.toString()); |
| 161 | + remoteMessage.setMessageType(messageType); |
| 162 | + ReadableMapKeySetIterator iterator = params.keySetIterator(); |
| 163 | + while (iterator.hasNextKey()) { |
| 164 | + String key = iterator.nextKey(); |
| 165 | + ReadableType type = params.getType(key); |
| 166 | + if (type == ReadableType.String) { |
| 167 | + remoteMessage.addData(key, params.getString(key)); |
| 168 | + Log.d(TAG, "Firebase send: " + key); |
| 169 | + Log.d(TAG, "Firebase send: " + params.getString(key)); |
| 170 | + } |
| 171 | + } |
| 172 | + fm.send(remoteMessage.build()); |
| 173 | + } |
| 174 | + |
| 175 | + private void initSendHandler() { |
| 176 | + getReactApplicationContext().registerReceiver(new BroadcastReceiver() { |
| 177 | + @Override |
| 178 | + public void onReceive(Context context, Intent intent) { |
| 179 | + WritableMap params = Arguments.createMap(); |
| 180 | + if (intent.getBooleanExtra("hasError", false)) { |
| 181 | + WritableMap error = Arguments.createMap(); |
| 182 | + error.putInt("code", intent.getIntExtra("errCode", 0)); |
| 183 | + error.putString("message", intent.getStringExtra("errorMessage")); |
| 184 | + params.putMap("err", error); |
| 185 | + } else { |
| 186 | + params.putNull("err"); |
| 187 | + } |
| 188 | + FirestackUtils.sendEvent(mReactContext, EVENT_NAME_SEND, params); |
| 189 | + } |
| 190 | + }, mReceiveSendIntentFilter); |
| 191 | + } |
| 192 | +} |
0 commit comments