Skip to content

Commit 15198dc

Browse files
Messaging Token API for review (#6313)
1 parent c5501e6 commit 15198dc

File tree

10 files changed

+327
-95
lines changed

10 files changed

+327
-95
lines changed

Firebase/InstanceID/FIRInstanceID.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,11 @@ - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
385385

386386
FIRInstanceIDDeleteTokenHandler newHandler = ^(NSError *error) {
387387
// If a default token is deleted successfully, reset the defaultFCMToken too.
388-
if (!error && [self isDefaultTokenWithAuthorizedEntity:authorizedEntity scope:scope]) {
389-
self.defaultFCMToken = nil;
388+
if (!error) {
389+
if ([self isDefaultTokenWithAuthorizedEntity:authorizedEntity scope:scope] ||
390+
[authorizedEntity isEqualToString:@"*"]) {
391+
self.defaultFCMToken = nil;
392+
}
390393
}
391394
dispatch_async(dispatch_get_main_queue(), ^{
392395
handler(error);
@@ -523,7 +526,7 @@ - (void)deleteIdentityWithHandler:(FIRInstanceIDDeleteHandler)handler {
523526
return;
524527
}
525528

526-
[self.tokenManager.authService resetCheckinWithHandler:^(NSError *error) {
529+
[self deleteCheckinWithHandler:^(NSError *_Nullable error) {
527530
if (error) {
528531
if (handler) {
529532
handler(error);
@@ -550,6 +553,10 @@ - (void)deleteIdentityWithHandler:(FIRInstanceIDDeleteHandler)handler {
550553

551554
#pragma mark - Checkin
552555

556+
- (void)deleteCheckinWithHandler:(void (^)(NSError *error))handler {
557+
[self.tokenManager.authService resetCheckinWithHandler:handler];
558+
}
559+
553560
- (BOOL)tryToLoadValidCheckinInfo {
554561
FIRInstanceIDCheckinPreferences *checkinPreferences =
555562
[self.tokenManager.authService checkinPreferences];

Firebase/InstanceID/Private/FIRInstanceID_Private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ NS_ASSUME_NONNULL_BEGIN
6969
*/
7070
- (BOOL)tryToLoadValidCheckinInfo;
7171

72+
/**
73+
* Deletes the checkin info for the app.
74+
*
75+
* @param handler The completion handler to invoke once the request has completed.
76+
*/
77+
- (void)deleteCheckinWithHandler:(void (^)(NSError *_Nullable error))handler;
78+
7279
@end
7380

7481
NS_ASSUME_NONNULL_END

FirebaseInstanceID.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'FirebaseInstanceID'
3-
s.version = '4.6.0'
3+
s.version = '4.7.0'
44
s.summary = 'Firebase InstanceID'
55

66
s.description = <<-DESC

FirebaseMessaging.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'FirebaseMessaging'
3-
s.version = '4.6.2'
3+
s.version = '4.7.0'
44
s.summary = 'Firebase Messaging'
55

66
s.description = <<-DESC
@@ -55,7 +55,7 @@ device, and it is completely free.
5555
s.osx.framework = 'SystemConfiguration'
5656
s.weak_framework = 'UserNotifications'
5757
s.dependency 'FirebaseCore', '~> 6.10'
58-
s.dependency 'FirebaseInstanceID', '~> 4.6'
58+
s.dependency 'FirebaseInstanceID', '~> 4.7'
5959
s.dependency 'GoogleUtilities/AppDelegateSwizzler', '~> 6.7'
6060
s.dependency 'GoogleUtilities/Reachability', '~> 6.7'
6161
s.dependency 'GoogleUtilities/Environment', '~> 6.7'

FirebaseMessaging/Apps/Sample/Sample/ContentView.swift

Lines changed: 131 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct ContentView: View {
3333
Text("InstanceID")
3434
.font(.subheadline)
3535
.fontWeight(.semibold)
36+
3637
Text(identity.instanceID ?? "None").foregroundColor(.green)
3738
}
3839

@@ -55,68 +56,142 @@ struct ContentView: View {
5556
Text("Topic")
5657
.fontWeight(.semibold)
5758
}
58-
}
59-
.navigationBarTitle("Firebase Messaging")
60-
.foregroundColor(.blue)
6159

62-
// MARK: Action buttons
60+
// MARK: Action buttons
6361

64-
Button(action: getToken) {
65-
HStack {
66-
Image(systemName: "arrow.clockwise.circle.fill").font(.body)
67-
Text("Get ID and Token")
62+
VStack(alignment: .leading) {
63+
Text("getToken")
6864
.fontWeight(.semibold)
69-
}
70-
}
65+
HStack {
66+
Button(action: getIDAndToken) {
67+
HStack {
68+
Image(systemName: "arrow.clockwise.circle.fill")
69+
Text("IID.ID")
70+
.fontWeight(.semibold)
71+
}
72+
}
73+
Button(action: getToken) {
74+
HStack {
75+
Image(systemName: "arrow.clockwise.circle.fill")
76+
Text("IID")
77+
.fontWeight(.semibold)
78+
}
79+
}
80+
Button(action: getFCMToken) {
81+
HStack {
82+
Image(systemName: "arrow.clockwise.circle.fill").font(.body)
83+
Text("FM")
84+
.fontWeight(.semibold)
85+
}
86+
}
87+
}
88+
}.font(.system(size: 14))
7189

72-
Button(action: deleteToken) {
73-
HStack {
74-
Image(systemName: "trash.fill").font(.body)
75-
Text("Delete Token")
90+
VStack(alignment: .leading) {
91+
Text("deleteToken")
7692
.fontWeight(.semibold)
77-
}
78-
}
93+
HStack {
94+
Button(action: deleteToken) {
95+
HStack {
96+
Image(systemName: "trash.fill")
97+
Text("IID")
98+
.fontWeight(.semibold)
99+
}
100+
}
101+
Button(action: deleteFCMToken) {
102+
HStack {
103+
Image(systemName: "trash.fill")
104+
Text("FM")
105+
.fontWeight(.semibold)
106+
}
107+
}
108+
}
109+
}.font(.system(size: 14))
79110

80-
Button(action: deleteID) {
81-
HStack {
82-
Image(systemName: "trash.fill").font(.body)
83-
Text("Delete ID")
111+
VStack(alignment: .leading) {
112+
Text("delete")
84113
.fontWeight(.semibold)
85-
}
114+
HStack {
115+
Button(action: deleteID) {
116+
HStack {
117+
Image(systemName: "trash.fill")
118+
Text("IID")
119+
.fontWeight(.bold)
120+
}
121+
}
122+
Button(action: deleteFCM) {
123+
HStack {
124+
Image(systemName: "trash.fill")
125+
Text("FM")
126+
.fontWeight(.semibold)
127+
}
128+
}
129+
Button(action: deleteFID) {
130+
HStack {
131+
Image(systemName: "trash.fill")
132+
Text("FIS")
133+
.fontWeight(.semibold)
134+
}
135+
}
136+
}
137+
}.font(.system(size: 14))
138+
Text("\(log)")
139+
.lineLimit(10)
140+
.multilineTextAlignment(.leading)
86141
}
142+
.navigationBarTitle("Firebase Messaging")
87143

88-
Button(action: deleteFID) {
89-
HStack {
90-
Image(systemName: "trash.fill").font(.body)
91-
Text("Delete FID")
92-
.fontWeight(.semibold)
93-
}
94-
}
95-
Text("\(log)")
96-
.lineLimit(10)
97-
.multilineTextAlignment(.leading)
98144
}.buttonStyle(IdentityButtonStyle())
99145
}
100146
}
101147

102-
func getToken() {
148+
func getIDAndToken() {
103149
InstanceID.instanceID().instanceID { result, error in
104150
guard let result = result, error == nil else {
105151
self.log = "Failed getting iid and token: \(String(describing: error))"
106152
return
107153
}
108154
self.identity.token = result.token
109155
self.identity.instanceID = result.instanceID
110-
self.log = "Successfully got token."
156+
self.log = "Successfully got iid and token."
111157
}
112158
}
113159

114-
func deleteToken() {
160+
func getToken() {
115161
guard let app = FirebaseApp.app() else {
116162
return
117163
}
118164
let senderID = app.options.gcmSenderID
119-
Messaging.messaging().deleteFCMToken(forSenderID: senderID) { error in
165+
var options: [String: Any] = [:]
166+
if Messaging.messaging().apnsToken == nil {
167+
log = "There's no APNS token available at the moment."
168+
return
169+
}
170+
options = ["apns_token": Messaging.messaging().apnsToken as Any]
171+
InstanceID.instanceID()
172+
.token(withAuthorizedEntity: senderID, scope: "*", options: options) { token, error in
173+
guard let token = token, error == nil else {
174+
self.log = "Failed getting token: \(String(describing: error))"
175+
return
176+
}
177+
self.identity.token = token
178+
self.log = "Successfully got token."
179+
}
180+
}
181+
182+
func getFCMToken() {
183+
Messaging.messaging().token { token, error in
184+
guard let token = token, error == nil else {
185+
self.log = "Failed getting iid and token: \(String(describing: error))"
186+
return
187+
}
188+
self.identity.token = token
189+
self.log = "Successfully got token."
190+
}
191+
}
192+
193+
func deleteFCMToken() {
194+
Messaging.messaging().deleteToken { error in
120195
if let error = error as NSError? {
121196
self.log = "Failed deleting token: \(error)"
122197
return
@@ -125,6 +200,16 @@ struct ContentView: View {
125200
}
126201
}
127202

203+
func deleteToken() {
204+
guard let app = FirebaseApp.app() else {
205+
return
206+
}
207+
let senderID = app.options.gcmSenderID
208+
InstanceID.instanceID()
209+
.deleteToken(withAuthorizedEntity: senderID, scope: "*") { error in
210+
}
211+
}
212+
128213
func deleteID() {
129214
InstanceID.instanceID().deleteID { error in
130215
if let error = error as NSError? {
@@ -135,6 +220,16 @@ struct ContentView: View {
135220
}
136221
}
137222

223+
func deleteFCM() {
224+
Messaging.messaging().deleteData { error in
225+
if let error = error as NSError? {
226+
self.log = "Failed deleting Messaging: \(error)"
227+
return
228+
}
229+
self.log = "Successfully deleted Messaging data."
230+
}
231+
}
232+
138233
func deleteFID() {
139234
Installations.installations().delete { error in
140235
if let error = error as NSError? {
@@ -199,7 +294,7 @@ struct ContentView_Previews: PreviewProvider {
199294
struct IdentityButtonStyle: ButtonStyle {
200295
func makeBody(configuration: Self.Configuration) -> some View {
201296
configuration.label
202-
.frame(minWidth: 0, maxWidth: 200)
297+
.frame(minWidth: 0, maxWidth: 60)
203298
.padding()
204299
.foregroundColor(.white)
205300
.background(Color.yellow)

FirebaseMessaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 2020-09 -- v.4.7.0
2+
- [added] Added new token APIs to get and delete the default FCM registration token asynchronously. Also added a new `Messaging.delete(completion:)` method that deletes all FCM registration tokens and checkin data. (#6313)
3+
14
# 2020-08 -- v.4.6.2
25
- [fixed] Fixed an issue that topic doesn't work in watchOS. (#6160)
36
- [fixed] Improved Xcode completion of public API completion handlers in Swift. (#6278)

FirebaseMessaging/Sources/FIRMessaging.m

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,15 @@ - (NSString *)FCMToken {
561561
return token;
562562
}
563563

564+
- (void)tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
565+
FIROptions *options = FIRApp.defaultApp.options;
566+
[self retrieveFCMTokenForSenderID:options.GCMSenderID completion:completion];
567+
}
568+
- (void)deleteTokenWithCompletion:(FIRMessagingDeleteFCMTokenCompletion)completion {
569+
FIROptions *options = FIRApp.defaultApp.options;
570+
[self deleteFCMTokenForSenderID:options.GCMSenderID completion:completion];
571+
}
572+
564573
- (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
565574
completion:(nonnull FIRMessagingFCMTokenFetchCompletion)completion {
566575
if (!senderID.length) {
@@ -611,6 +620,38 @@ - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
611620
handler:completion];
612621
}
613622

623+
- (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
624+
FIRMessaging_WEAKIFY(self);
625+
[self.instanceID
626+
deleteTokenWithAuthorizedEntity:@"*"
627+
scope:@"*"
628+
handler:^(NSError *_Nonnull error) {
629+
FIRMessaging_STRONGIFY(self);
630+
if (error) {
631+
completion(error);
632+
return;
633+
}
634+
[self.instanceID
635+
deleteCheckinWithHandler:^(NSError *_Nullable error) {
636+
if (error) {
637+
completion(error);
638+
return;
639+
}
640+
// Only request new token if FCM auto initialization is
641+
// enabled.
642+
if ([self isAutoInitEnabled]) {
643+
// Deletion succeeds! Requesting new checkin, IID and token.
644+
[self tokenWithCompletion:^(NSString *_Nullable token,
645+
NSError *_Nullable error) {
646+
completion(error);
647+
}];
648+
return;
649+
}
650+
completion(nil);
651+
}];
652+
}];
653+
}
654+
614655
#pragma mark - FIRMessagingDelegate helper methods
615656
- (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
616657
_delegate = delegate;

0 commit comments

Comments
 (0)