Skip to content

Commit ad68664

Browse files
committed
Initial attemp to migrate to FCM.
1 parent fe1dc56 commit ad68664

File tree

6 files changed

+136
-99
lines changed

6 files changed

+136
-99
lines changed

README.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ In `android/app/src/main/AndroidManifest.xml`
9090
```xml
9191
<application
9292
xmlns:tools="http://schemas.android.com/tools"
93-
tools:replace="android:icon"
93+
tools:replace="android:icon,android:allowBackup"
9494
...>
9595
</application>
9696
```
@@ -108,17 +108,35 @@ include ':react-native-azurenotificationhub'
108108
project(':react-native-azurenotificationhub').projectDir = file('../node_modules/react-native-azurenotificationhub/android')
109109
```
110110

111+
In `android/build.gradle`
112+
113+
```gradle
114+
...
115+
116+
buildscript {
117+
...
118+
dependencies {
119+
...
120+
classpath 'com.google.gms:google-services:4.2.0'
121+
}
122+
}
123+
124+
```
125+
111126
In `android/app/build.gradle`
112127

113128
```gradle
114129
...
115130
dependencies {
116131
...
117132
118-
compile project(':react-native-azurenotificationhub')
119-
compile 'com.google.android.gms:play-services-gcm:9.4.0'
120-
compile 'com.google.firebase:firebase-messaging:9.4.0'
133+
implementation project(':react-native-azurenotificationhub')
134+
implementation 'com.google.firebase:firebase-messaging:17.6.0'
135+
implementation 'com.google.firebase:firebase-core:16.0.8'
121136
}
137+
138+
apply plugin: 'com.google.gms.google-services'
139+
122140
```
123141

124142
In `android/app/src/main/AndroidManifest.xml`
@@ -127,21 +145,28 @@ In `android/app/src/main/AndroidManifest.xml`
127145
...
128146

129147
<uses-permission android:name="android.permission.INTERNET"/>
130-
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
131-
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
132148

133149
<application ...>
134150
...
135-
<service
136-
android:name="com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService"
137-
android:exported="false">
138-
</service>
139-
<receiver android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
140-
android:permission="com.google.android.c2dm.permission.SEND">
141-
<intent-filter>
142-
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
143-
</intent-filter>
144-
</receiver>
151+
<service
152+
android:name="com.azure.reactnative.notificationhub.ReactNativeRegistrationIntentService"
153+
android:exported="false" />
154+
155+
<service
156+
android:name="com.azure.reactnative.notificationhub.ReactNativeFirebaseMessagingService"
157+
android:stopWithTask="false">
158+
<intent-filter>
159+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
160+
</intent-filter>
161+
</service>
162+
163+
<receiver
164+
android:name="com.microsoft.windowsazure.notifications.NotificationsBroadcastReceiver"
165+
android:permission="com.google.android.c2dm.permission.SEND">
166+
<intent-filter>
167+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
168+
</intent-filter>
169+
</receiver>
145170
...
146171
```
147172

android/build.gradle

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 26
5-
buildToolsVersion "26.0.3"
65

76
defaultConfig {
87
minSdkVersion 16
@@ -22,13 +21,13 @@ android {
2221
}
2322

2423
dependencies {
25-
compile fileTree(dir: 'libs', include: ['*.jar'])
26-
compile 'com.facebook.react:react-native:+'
27-
compile 'com.google.android.gms:play-services-gcm:15.0.0'
28-
compile 'com.google.firebase:firebase-messaging:15.0.0'
29-
compile 'com.android.support:appcompat-v7:26.1.0'
30-
compile 'com.microsoft.azure:notification-hubs-android-sdk:0.4@aar'
31-
compile 'com.microsoft.azure:azure-notifications-handler:1.0.1@aar'
24+
implementation fileTree(dir: 'libs', include: ['*.jar'])
25+
implementation 'com.facebook.react:react-native:+'
26+
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
27+
implementation 'com.google.firebase:firebase-messaging:17.6.0'
28+
implementation 'com.android.support:appcompat-v7:26.1.0'
29+
implementation 'com.microsoft.azure:notification-hubs-android-sdk:0.4@aar'
30+
implementation 'com.microsoft.azure:azure-notifications-handler:1.0.1@aar'
3231
}
3332

3433
repositories {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.azure.reactnative.notificationhub;
2+
3+
import android.content.Intent;
4+
import android.util.Log;
5+
6+
import com.google.firebase.messaging.FirebaseMessagingService;
7+
8+
public class ReactNativeFirebaseMessagingService extends FirebaseMessagingService {
9+
10+
private static final String TAG = "ReactNativeFirebaseMS";
11+
12+
@Override
13+
public void onNewToken(String token) {
14+
Log.i(TAG, "Refreshing FCM Registration Token");
15+
16+
Intent intent = new Intent(this, ReactNativeRegistrationIntentService.class);
17+
startService(intent);
18+
}
19+
}

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeInstanceIDService.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationsHandler.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
import java.util.Set;
3333

3434
public class ReactNativeNotificationsHandler extends NotificationsHandler {
35-
public static final String TAG = "ReactNativeNotificationsHandler";
35+
public static final String TAG = "ReactNativeNotification";
36+
3637
private static final String NOTIFICATION_CHANNEL_ID = "rn-push-notification-channel-id";
3738
private static final long DEFAULT_VIBRATION = 300L;
3839

@@ -48,24 +49,24 @@ public void onReceive(Context context, Bundle bundle) {
4849
public void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
4950
(new Thread() {
5051
public void run() {
51-
try
52-
{
52+
try {
5353
Thread.currentThread().sleep(delay);
54-
JSONObject json = new JSONObject();
55-
Set<String> keys = bundle.keySet();
56-
for (String key : keys) {
57-
try {
58-
json.put(key, bundle.get(key));
59-
} catch (JSONException e) {}
60-
}
54+
JSONObject json = new JSONObject();
55+
Set<String> keys = bundle.keySet();
56+
for (String key : keys) {
57+
try {
58+
json.put(key, bundle.get(key));
59+
} catch (JSONException e) {
60+
}
61+
}
6162

62-
Intent event= new Intent(TAG);
63-
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
64-
event.putExtra("data", json.toString());
63+
Intent event = new Intent(TAG);
64+
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
65+
event.putExtra("data", json.toString());
6566
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
66-
localBroadcastManager.sendBroadcast(event);
67+
localBroadcastManager.sendBroadcast(event);
68+
} catch (Exception e) {
6769
}
68-
catch (Exception e) {}
6970
}
7071
}).start();
7172
}
@@ -291,14 +292,15 @@ private NotificationManager notificationManager() {
291292
}
292293

293294
private static boolean channelCreated = false;
295+
294296
private static void checkOrCreateChannel(NotificationManager manager) {
295297
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
296298
return;
297299
if (channelCreated)
298300
return;
299301
if (manager == null)
300302
return;
301-
final CharSequence name = "rn-push-notification-channel";
303+
final CharSequence name = "rn-push-notification-channel";
302304
int importance = NotificationManager.IMPORTANCE_DEFAULT;
303305
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance);
304306
channel.enableLights(true);

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,76 @@
55
import android.support.v4.content.LocalBroadcastManager;
66
import android.util.Log;
77

8-
import com.google.firebase.FirebaseApp;
9-
import com.google.firebase.FirebaseOptions;
8+
import com.google.android.gms.tasks.OnSuccessListener;
109
import com.google.firebase.iid.FirebaseInstanceId;
10+
import com.google.firebase.iid.InstanceIdResult;
1111
import com.microsoft.windowsazure.messaging.NotificationHub;
1212

13+
import java.util.concurrent.ExecutorService;
14+
import java.util.concurrent.Executors;
15+
1316
public class ReactNativeRegistrationIntentService extends IntentService {
1417

1518
public static final String TAG = "ReactNativeRegistration";
1619

20+
private final ExecutorService pool = Executors.newFixedThreadPool(1);
21+
1722
public ReactNativeRegistrationIntentService() {
1823
super(TAG);
1924
}
2025

2126
@Override
2227
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);
3335

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+
}
4042

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);
4351

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);
5159

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);
5362

54-
notificationHubUtil.setRegistrationID(this, regID);
55-
notificationHubUtil.setFCMToken(this, token);
63+
notificationHubUtil.setRegistrationID(ReactNativeRegistrationIntentService.this, regID);
64+
notificationHubUtil.setFCMToken(ReactNativeRegistrationIntentService.this, token);
5665

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);
6372

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+
});
6879
}
6980
}

0 commit comments

Comments
 (0)