Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get token from messaging sdk #1053

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions messaging/MessagingExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="No InstanceID token" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="5" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1wF-0v-e01">
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="No FCM registration token" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="5" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1wF-0v-e01">
<rect key="frame" x="67.5" y="565" width="240" height="13.5"/>
<constraints>
<constraint firstAttribute="width" constant="240" id="FXq-Tm-sRh"/>
Expand All @@ -83,7 +83,7 @@
<navigationItem key="navigationItem" title="FCM Example" id="uI5-jL-nrY"/>
<connections>
<outlet property="fcmTokenMessage" destination="RBg-uJ-gCV" id="FUs-GZ-mfF"/>
<outlet property="instanceIDTokenMessage" destination="1wF-0v-e01" id="Dmb-Lh-hLD"/>
<outlet property="fcmRegTokenMessage" destination="1wF-0v-e01" id="Dmb-Lh-hLD"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
Expand Down
2 changes: 1 addition & 1 deletion messaging/MessagingExample/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *fcmTokenMessage;
@property (weak, nonatomic) IBOutlet UILabel *instanceIDTokenMessage;
@property (weak, nonatomic) IBOutlet UILabel *fcmRegTokenMessage;
@end
19 changes: 8 additions & 11 deletions messaging/MessagingExample/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,20 @@ - (IBAction)handleLogTokenTouch:(id)sender {
NSString *fcmToken = [FIRMessaging messaging].FCMToken;
NSLog(@"Local FCM registration token: %@", fcmToken);
// [END log_fcm_reg_token]

NSString* displayToken = [NSString stringWithFormat:@"Logged FCM token: %@", fcmToken];
self.fcmTokenMessage.text = displayToken;

// [START log_iid_reg_token]
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
NSError * _Nullable error) {

// [START log_reg_token]
[[FIRMessaging messaging] tokenWithCompletion:^(NSString *token, NSError *error) {
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
NSLog(@"Error getting FCM registration token: %@", error);
} else {
NSLog(@"Remote instance ID token: %@", result.token);
NSString* message =
[NSString stringWithFormat:@"Remote InstanceID token: %@", result.token];
self.instanceIDTokenMessage.text = message;
NSLog(@"FCM registration token: %@", token);
self.fcmRegTokenMessage.text = token;
}
}];
// [END log_iid_reg_token]
// [END log_reg_token]
}

- (IBAction)handleSubscribeTouch:(id)sender {
Expand Down
22 changes: 11 additions & 11 deletions messaging/MessagingExampleSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,32 @@ import Firebase

@objc(ViewController)
class ViewController: UIViewController {

@IBOutlet weak var fcmTokenMessage: UILabel!
@IBOutlet weak var instanceIDTokenMessage: UILabel!
@IBOutlet weak var fcmRegTokenMessage: UILabel!

override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(self.displayFCMToken(notification:)),
name: Notification.Name("FCMToken"), object: nil)
}

@IBAction func handleLogTokenTouch(_ sender: UIButton) {
// [START log_fcm_reg_token]
let token = Messaging.messaging().fcmToken
print("FCM token: \(token ?? "")")
// [END log_fcm_reg_token]
self.fcmTokenMessage.text = "Logged FCM token: \(token ?? "")"

// [START log_iid_reg_token]
InstanceID.instanceID().instanceID { (result, error) in
// [START log_reg_token]
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching remote instance ID: \(error)")
} else if let result = result {
print("Remote instance ID token: \(result.token)")
self.instanceIDTokenMessage.text = "Remote InstanceID token: \(result.token)"
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
self.fcmRegTokenMessage.text = "Remote FCM registration token: \(token)"
}
}
// [END log_iid_reg_token]
// [END log_reg_token]
}

@IBAction func handleSubscribeTouch(_ sender: UIButton) {
Expand Down
6 changes: 3 additions & 3 deletions messaging/MessagingExampleUITests/MessagingExampleUITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ - (void)testVerifyAppLaunched {

// Wait till network call is finished.
NSPredicate *gone = [NSPredicate predicateWithFormat:@"exists == false"];
FIRWaitForPredicateWithTimeout(gone, [_app staticTexts][@"No InstanceID token"], 20);
FIRWaitForPredicateWithTimeout(gone, [_app staticTexts][@"No FCM registration token"], 20);

// Make sure [FIRMessaging messaging].FCMToken returns FCM token.
XCTAssertTrue(
[[[[_app staticTexts] elementBoundByIndex:1] label] containsString:@"Logged FCM token"]);
// Make sure [FIRInstanceID instanceID] instanceIDWithHandler: returns FCM token.
// Make sure [FIRMessaging messaging] tokenWithCompletion: returns FCM token.
XCTAssertTrue([[[[_app staticTexts] elementBoundByIndex:2] label]
containsString:@"Remote InstanceID token"]);
containsString:@"Remote FCM registration token"]);
}

@end