3

I wonder, is there another way to get the push device token without the delegate method:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken

because I used OneSignal, and they don't need to conform that method to get the device token.

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
kent2508
  • 79
  • 7

2 Answers2

4

You can get the push token for OneSignal SDK like this:

 OneSignal.promptForPushNotifications(userResponse: { accepted in
  print("User accepted notifications: \(accepted)")
            
  if let deviceState = OneSignal.getDeviceState() {
    let userId = deviceState.userId
    let pushToken = deviceState.pushToken
    let subscribed = deviceState.isSubscribed
    print("pushToken ====> \(pushToken)") //Here you can get the token
  }
 })

Keep coding........... :)

Krishna Raj Salim
  • 7,331
  • 5
  • 34
  • 66
2

OneSignal API states IdsAvailable is the method you would use:

[OneSignal IdsAvailable:^(NSString* userId, NSString* pushToken) {
    NSLog(@"UserId:%@", userId);
    if (pushToken != nil)
        NSLog(@"pushToken:%@", pushToken);
}];

Is the method to get the token; mentioned here.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • where u called this – Anbu.Karthik Mar 03 '17 at 05:54
  • @Anbu.Karthik: I'm pretty sure it's called in `- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions` (see the page I linked). – l'L'l Mar 03 '17 at 05:55
  • see this `[OneSignal registerForPushNotifications];` you can get the documentation on https://documentation.onesignal.com/docs/ios-native-sdk#section--registerforpushnotifications- – Anbu.Karthik Mar 03 '17 at 06:00
  • @Anbu.Karthik: Yes, that's the same page I linked already; `registerForPushNotifications` does not return the `token`, it allows the `IdsAvailable` method to if it returns `true`. – l'L'l Mar 03 '17 at 06:02
  • I know the method `IdsAvailable`, I just wondering how OneSignal can get the device token without conform the method `- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(nonnull NSData *)deviceToken` – kent2508 Mar 03 '17 at 06:12
  • 1
    @kent2508: Because you are going through their API, in which they are using the `APN` method on their server, which acts as a middleman. I would not doubt they use that exact method in their backend, then translate the token to their own which you then use. – l'L'l Mar 03 '17 at 06:14
  • @l'L'l Can you explain more detail about that backend method? – kent2508 Mar 03 '17 at 08:09
  • Instead of you calling `didRegisterForRemoteNotificationsWithDeviceToken` in your app OneSignal handles that on their end; in other words they take care of the interaction with Apples Push Notifications for your app instead of you. You then interact with the OneSignal API to get the APN notifications they are handling. That's pretty much it right there — a notifications middleman. – l'L'l Mar 03 '17 at 10:14
  • @I'L'l: I knew! I used to NSNotificationCenter to handle some AppDelegate methods like: UIApplicationDidBecomeActiveNotification, UIApplicationWillTerminateNotification, UIApplicationWillEnterForegroundNotification, ... but I found no nofication keyword to handle push device token :( Can you show me how? – kent2508 Mar 24 '17 at 08:43