import jwt from 'jsonwebtoken';const APP_ID = process.env['ONESIGNAL_APP_ID']const IDENTITY_VERIFICATION_SECRET = process.env['ONESIGNAL_IDENTITY_VERIFICATION_SECRET_KEY']// Generates JWT, potentially with subscription claims, for the user identified by the External IDfunction signOneSignalJWT(externalId, subscriptions) {return jwt.sign({iss: APP_ID,exp: Math.floor(Date.now() / 1000) + 3600, // 1-hour expirationidentity: {'external_id': externalId,},subscriptions},IDENTITY_VERIFICATION_SECRET,{ algorithm: 'ES256' });}// Pass this token to your mobile app to use with the `login` SDK methodconst onesignalJWT = signOneSignalJWT('EXTERNAL_ID');
OneSignal.addUserJwtInvalidatedListener(event -> { // Get the expired user's External ID String externalId = event.getExternalId(); // Fetch a new JWT from your backend for the user String onesignalJWT = "yourUpdatedToken"; // Provide the new JWT to the SDK OneSignal.updateUserJwt(externalId, onesignalJWT);});
OneSignal.addUserJwtInvalidatedListener( object : IUserJwtInvalidatedListener { override fun onUserJwtInvalidated(event: UserJwtInvalidatedEvent) { val externalId = event.externalId val onesignalJWT = "" updateUserJwt(externalId, onesignalJWT) } },)
class AppDelegate: UIResponder, UIApplicationDelegate, OSUserJwtInvalidatedListener { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { // Set self to listen for JWT invalidated events OneSignal.addUserJwtInvalidatedListener(self) // Remove the JWT listener as needed by calling: // `OneSignal.removeUserJwtInvalidatedListener(self)` } // Required to conform to `OSUserJwtInvalidatedListener` protocol func onUserJwtInvalidated(event: OneSignalUser.OSUserJwtInvalidatedEvent) { // Get the expired user's External ID let externalId = event.externalId // Fetch a new JWT from your backend for the user let onesignalJWT = "yourUpdatedToken" // Provide the new JWT to the SDK OneSignal.updateUserJwt(externalId: externalId, token: onesignalJWT) }}
@interface MyListener: NSObject<OSUserJwtInvalidatedListener>@end@implementation MyListener- (void)onUserJwtInvalidatedWithEvent:(OSUserJwtInvalidatedEvent * _Nonnull)event { // Get the expired user's External ID NSString *externalId = event.externalId; // Fetch a new JWT from your backend for the user NSString *onesignalJWT = @"yourUpdatedToken"; // Provide the new JWT to the SDK [OneSignal updateUserJwt:externalId withToken:onesignalJWT];}// Add or remove your User Jwt Invalidated Listener[OneSignal addUserJwtInvalidatedListener:myListener];[OneSignal removeUserJwtInvalidatedListener:myListener];
// If you have not already included it in your JWT token, update the JWT with the emaillet onesignalJWT = "newTokenThatContainsTheEmailToAdd"OneSignal.updateUserJwt(externalId: externalId, token: onesignalJWT)// Add the emailOneSignal.User.addEmail(emailAddress)
// If you have not already included it in your JWT token, update the JWT with the emailNSString *onesignalJWT = @"newTokenThatContainsTheEmailToAdd";[OneSignal updateUserJwt:externalId withToken:onesignalJWT];// Add the email[OneSignal.User addEmail:emailAddress];
OneSignal.getUser().addSms(smsNumber);
OneSignal.getUser().addSms(smsNumber)
// If you have not already included it in your JWT token, update the JWT with the smslet onesignalJWT = "newTokenThatContainsTheSmsToAdd"OneSignal.updateUserJwt(externalId: externalId, token: onesignalJWT)// Add the sms numberOneSignal.User.addSms(smsNumber)
// If you have not already included it in your JWT token, update the JWT with the smsNSString *onesignalJWT = @"newTokenThatContainsTheSmsToAdd";[OneSignal updateUserJwt:externalId withToken:onesignalJWT];// Add the sms number[OneSignal.User addSms:smsNumber];