Skip to content

Commit b567364

Browse files
authored
Fetch FirebaseMessaging Token during NotificationHub.start call (#175)
* Extract Firebase Messaging version into a variable * Use FirebaseMessaging class instead of FirebaseInstanceId * Make PushChannel bootstrapping code available Previously, we only fetched the FirebaseMessaging token in the FirebaseReceiver. This lead to a bug where bootstrapping didn't occur during app-upgrades where new tokens are not issued, hence the FirebaseReceiver is never instantiated. With this change, the FirebaseMessaging ID will be re-evaluated when the NotificatonHub.start method is called, and/or when the FirebaseReceiver is started. * Bring 'general' copy of FirebaseReceiver to parity with 'fcm' copy * Removing unused-imports
1 parent 6e20e2e commit b567364

File tree

6 files changed

+94
-44
lines changed

6 files changed

+94
-44
lines changed

notification-hubs-sdk/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ configurations {
6262
dependencies {
6363
implementation fileTree(dir: 'libs', include: ['*.jar'])
6464
implementation 'com.android.volley:volley:1.1.1'
65-
fcmApi 'com.google.firebase:firebase-messaging:21.0.1'
66-
generalApi 'com.google.firebase:firebase-messaging:21.0.1'
65+
66+
def firebaseMessagingVersion = '21.0.1'
67+
fcmApi "com.google.firebase:firebase-messaging:${firebaseMessagingVersion}"
68+
generalApi "com.google.firebase:firebase-messaging:${firebaseMessagingVersion}"
6769

6870
javadocDeps 'com.android.support:support-annotations:28.0.0'
6971

notification-hubs-sdk/src/fcm/java/com/microsoft/windowsazure/messaging/notificationhubs/FirebaseReceiver.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package com.microsoft.windowsazure.messaging.notificationhubs;
22

3-
import android.util.Log;
4-
53
import androidx.annotation.NonNull;
64

7-
import com.google.android.gms.tasks.OnCompleteListener;
8-
import com.google.android.gms.tasks.Task;
9-
import com.google.firebase.iid.FirebaseInstanceId;
10-
import com.google.firebase.iid.InstanceIdResult;
115
import com.google.firebase.messaging.FirebaseMessagingService;
126
import com.google.firebase.messaging.RemoteMessage;
137

@@ -47,21 +41,6 @@ public void onCreate() {
4741
super.onCreate();
4842

4943
mHub.registerApplication(this.getApplication());
50-
51-
if (mHub.getInstancePushChannel() == null) {
52-
FirebaseInstanceId.getInstance()
53-
.getInstanceId()
54-
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
55-
@Override
56-
public void onComplete(@NonNull Task<InstanceIdResult> task) {
57-
if (!task.isSuccessful()) {
58-
Log.e("ANH", "unable to fetch FirebaseInstanceId");
59-
return;
60-
}
61-
mHub.setInstancePushChannel(task.getResult().getToken());
62-
}
63-
});
64-
}
6544
}
6645

6746
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.microsoft.windowsazure.messaging.notificationhubs;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.util.Log;
7+
8+
import androidx.annotation.NonNull;
9+
10+
import com.google.android.gms.tasks.OnCompleteListener;
11+
import com.google.android.gms.tasks.Task;
12+
import com.google.firebase.messaging.FirebaseMessaging;
13+
14+
/**
15+
* Allows platform-specific functionality to be made available to the NotificationHub global single
16+
* instance.
17+
*/
18+
class NotificationHubExtension {
19+
/**
20+
* Queries Firebase for the most recent token asynchronously.
21+
*
22+
* @param hub The instance of {@link NotificationHub} that should be made aware of the token.
23+
*/
24+
public static void fetchPushChannel(final NotificationHub hub) {
25+
/*
26+
* Keeping this out of the NotificationHub class allows us to keep platform-specific knowledge
27+
* from entering the shared-portion of the code-base.
28+
*
29+
* Keeping this out of the FirebaseReceiver class allows us to not rely on Service start-time
30+
* behavior, which caused an issue for people adopting our platform:
31+
* https://github.com/Azure/azure-notificationhubs-xamarin/issues/21
32+
*/
33+
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
34+
@Override
35+
public void onComplete(@NonNull Task<String> task) {
36+
if (!task.isSuccessful()) {
37+
Log.e("ANH", "unable to fetch FirebaseInstanceId");
38+
return;
39+
}
40+
hub.setInstancePushChannel(task.getResult());
41+
}
42+
});
43+
}
44+
}

notification-hubs-sdk/src/general/java/com/microsoft/windowsazure/messaging/notificationhubs/FirebaseReceiver.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
package com.microsoft.windowsazure.messaging.notificationhubs;
22

3-
import android.util.Log;
4-
53
import androidx.annotation.NonNull;
64

7-
import com.google.android.gms.tasks.OnCompleteListener;
8-
import com.google.android.gms.tasks.Task;
9-
import com.google.firebase.iid.FirebaseInstanceId;
10-
import com.google.firebase.iid.InstanceIdResult;
115
import com.google.firebase.messaging.FirebaseMessagingService;
126
import com.google.firebase.messaging.RemoteMessage;
137

@@ -47,21 +41,6 @@ public void onCreate() {
4741
super.onCreate();
4842

4943
mHub.registerApplication(this.getApplication());
50-
51-
if (mHub.getInstancePushChannel() == null) {
52-
FirebaseInstanceId.getInstance()
53-
.getInstanceId()
54-
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
55-
@Override
56-
public void onComplete(@NonNull Task<InstanceIdResult> task) {
57-
if (!task.isSuccessful()) {
58-
Log.e("ANH", "unable to fetch FirebaseInstanceId");
59-
return;
60-
}
61-
mHub.setInstancePushChannel(task.getResult().getToken());
62-
}
63-
});
64-
}
6544
}
6645

6746
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.microsoft.windowsazure.messaging.notificationhubs;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.util.Log;
7+
8+
import androidx.annotation.NonNull;
9+
10+
import com.google.android.gms.tasks.OnCompleteListener;
11+
import com.google.android.gms.tasks.Task;
12+
import com.google.firebase.messaging.FirebaseMessaging;
13+
14+
/**
15+
* Allows platform-specific functionality to be made available to the NotificationHub global single
16+
* instance.
17+
*/
18+
class NotificationHubExtension {
19+
/**
20+
* Queries Firebase for the most recent token asynchronously.
21+
*
22+
* @param hub The instance of {@link NotificationHub} that should be made aware of the token.
23+
*/
24+
public static void fetchPushChannel(final NotificationHub hub) {
25+
/*
26+
* Keeping this out of the NotificationHub class allows us to keep platform-specific knowledge
27+
* from entering the shared-portion of the code-base.
28+
*
29+
* Keeping this out of the FirebaseReceiver class allows us to not rely on Service start-time
30+
* behavior, which caused an issue for people adopting our platform:
31+
* https://github.com/Azure/azure-notificationhubs-xamarin/issues/21
32+
*/
33+
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
34+
@Override
35+
public void onComplete(@NonNull Task<String> task) {
36+
if (!task.isSuccessful()) {
37+
Log.e("ANH", "unable to fetch FirebaseInstanceId");
38+
return;
39+
}
40+
hub.setInstancePushChannel(task.getResult());
41+
}
42+
});
43+
}
44+
}

notification-hubs-sdk/src/main/java/com/microsoft/windowsazure/messaging/notificationhubs/NotificationHub.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ synchronized void registerApplication(Application application) {
9191

9292
mUserIdVisitor = new UserIdVisitor(mApplication);
9393
useInstanceVisitor(mUserIdVisitor);
94+
95+
NotificationHubExtension.fetchPushChannel(this);
9496
}
9597

9698
/**

0 commit comments

Comments
 (0)