|
5 | 5 | import android.support.v4.content.LocalBroadcastManager;
|
6 | 6 | import android.util.Log;
|
7 | 7 |
|
8 |
| -import com.google.firebase.FirebaseApp; |
9 |
| -import com.google.firebase.FirebaseOptions; |
| 8 | +import com.google.android.gms.tasks.OnSuccessListener; |
10 | 9 | import com.google.firebase.iid.FirebaseInstanceId;
|
| 10 | +import com.google.firebase.iid.InstanceIdResult; |
11 | 11 | import com.microsoft.windowsazure.messaging.NotificationHub;
|
12 | 12 |
|
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | + |
13 | 16 | public class ReactNativeRegistrationIntentService extends IntentService {
|
14 | 17 |
|
15 | 18 | public static final String TAG = "ReactNativeRegistration";
|
16 | 19 |
|
| 20 | + private final ExecutorService pool = Executors.newFixedThreadPool(1); |
| 21 | + |
17 | 22 | public ReactNativeRegistrationIntentService() {
|
18 | 23 | super(TAG);
|
19 | 24 | }
|
20 | 25 |
|
21 | 26 | @Override
|
22 | 27 | protected void onHandleIntent(Intent intent) {
|
23 |
| - LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); |
24 |
| - Intent event= new Intent(TAG); |
25 |
| - |
26 |
| - try { |
27 |
| - NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); |
28 |
| - String connectionString = notificationHubUtil.getConnectionString(this); |
29 |
| - String hubName = notificationHubUtil.getHubName(this); |
30 |
| - String regID = notificationHubUtil.getRegistrationID(this); |
31 |
| - String storedToken = notificationHubUtil.getFCMToken(this); |
32 |
| - String[] tags = notificationHubUtil.getTags(this); |
| 28 | + final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); |
| 29 | + final Intent event = new Intent(TAG); |
| 30 | + final NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); |
| 31 | + final String connectionString = notificationHubUtil.getConnectionString(this); |
| 32 | + final String hubName = notificationHubUtil.getHubName(this); |
| 33 | + final String storedToken = notificationHubUtil.getFCMToken(this); |
| 34 | + final String[] tags = notificationHubUtil.getTags(this); |
33 | 35 |
|
34 |
| - if (connectionString == null || hubName == null) { |
35 |
| - // The intent was triggered when no connection string has been set. |
36 |
| - // This is likely due to an InstanceID refresh occurring while no user |
37 |
| - // registration is active for Azure Notification Hub. |
38 |
| - return; |
39 |
| - } |
| 36 | + if (connectionString == null || hubName == null) { |
| 37 | + // The intent was triggered when no connection string has been set. |
| 38 | + // This is likely due to an InstanceID refresh occurring while no user |
| 39 | + // registration is active for Azure Notification Hub. |
| 40 | + return; |
| 41 | + } |
40 | 42 |
|
41 |
| - String token = FirebaseInstanceId.getInstance().getToken(); |
42 |
| - Log.d(TAG, "FCM Registration Token: " + token); |
| 43 | + FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( |
| 44 | + pool, new OnSuccessListener<InstanceIdResult>() { |
| 45 | + @Override |
| 46 | + public void onSuccess(InstanceIdResult instanceIdResult) { |
| 47 | + try { |
| 48 | + String regID = notificationHubUtil.getRegistrationID(ReactNativeRegistrationIntentService.this); |
| 49 | + String token = instanceIdResult.getToken(); |
| 50 | + Log.d(TAG, "FCM Registration Token: " + token); |
43 | 51 |
|
44 |
| - // Storing the registration ID indicates whether the generated token has been |
45 |
| - // sent to your server. If it is not stored, send the token to your server. |
46 |
| - // Also check if the token has been compromised and needs refreshing. |
47 |
| - if (regID == null || storedToken != token) { |
48 |
| - NotificationHub hub = new NotificationHub(hubName, connectionString, this); |
49 |
| - Log.d(TAG, "NH Registration refreshing with token : " + token); |
50 |
| - regID = hub.register(token, tags).getRegistrationId(); |
| 52 | + // Storing the registration ID indicates whether the generated token has been |
| 53 | + // sent to your server. If it is not stored, send the token to your server. |
| 54 | + // Also check if the token has been compromised and needs refreshing. |
| 55 | + if (regID == null || storedToken != token) { |
| 56 | + NotificationHub hub = new NotificationHub(hubName, connectionString, |
| 57 | + ReactNativeRegistrationIntentService.this); |
| 58 | + Log.d(TAG, "NH Registration refreshing with token : " + token); |
51 | 59 |
|
52 |
| - Log.d(TAG, "New NH Registration Successfully - RegId : " + regID); |
| 60 | + regID = hub.register(token, tags).getRegistrationId(); |
| 61 | + Log.d(TAG, "New NH Registration Successfully - RegId : " + regID); |
53 | 62 |
|
54 |
| - notificationHubUtil.setRegistrationID(this, regID); |
55 |
| - notificationHubUtil.setFCMToken(this, token); |
| 63 | + notificationHubUtil.setRegistrationID(ReactNativeRegistrationIntentService.this, regID); |
| 64 | + notificationHubUtil.setFCMToken(ReactNativeRegistrationIntentService.this, token); |
56 | 65 |
|
57 |
| - event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_REGISTER_AZURE_HUB_EVENT); |
58 |
| - event.putExtra("data", regID); |
59 |
| - localBroadcastManager.sendBroadcast(event); |
60 |
| - } |
61 |
| - } catch (Exception e) { |
62 |
| - Log.e(TAG, "Failed to complete token refresh", e); |
| 66 | + event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_REGISTER_AZURE_HUB_EVENT); |
| 67 | + event.putExtra("data", regID); |
| 68 | + localBroadcastManager.sendBroadcast(event); |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + Log.e(TAG, "Failed to complete token refresh", e); |
63 | 72 |
|
64 |
| - event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT); |
65 |
| - event.putExtra("data", e.getMessage()); |
66 |
| - localBroadcastManager.sendBroadcast(event); |
67 |
| - } |
| 73 | + event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT); |
| 74 | + event.putExtra("data", e.getMessage()); |
| 75 | + localBroadcastManager.sendBroadcast(event); |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
68 | 79 | }
|
69 | 80 | }
|
0 commit comments