Integrate with Push Notifications

Last Updated on : 2024-10-21 10:02:28download

The apps developed based on Smart Life App SDK support push notifications. You can integrate with this feature to send push notifications respecting promotional operations and product alerts to app users.

Configure a project

  1. Create a project in Xcode.

  2. Choose TARGETS > Capabilities and set Push Notifications to ON.

    Integrate with Push Notifications

  3. Log in to the Tuya Developer Platform and go to the SDK Development page. On the Certificates tab, you can configure and upload a push certificate.

    Integrate with Push Notifications

Initialize Push

Call the API method didFinishLaunchingWithOptions to initialize Push.

Example

Objective-C:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [application registerForRemoteNotifications];
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
        // Adds the following code block for iOS 10.
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        UNAuthorizationOptions types10 = UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
        [center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                // Allows this authorization.
            } else {
                // Rejects this authorization.
            }
        }];
    }

}

Swift:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    application.registerForRemoteNotifications()
    application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil))
    if #available(iOS 10.0, *) {
    // Adds the following code block for iOS 10.
        let center = UNUserNotificationCenter.current()
        center.delegate = self as? UNUserNotificationCenterDelegate
        let options: UNAuthorizationOptions = [.alert, .badge, .sound]
        center.requestAuthorization(options: options) { (granted, error) in
            if granted {
                // Allows this authorization.
            } else {
               // Rejects this authorization.
            }
        }
    }
}

Register a push notification ID

Registers pushId to Smart Life App SDK in didRegisterForRemoteNotificationsWithDeviceToken.

Example

Objective-C:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [ThingSmartSDK sharedInstance].deviceToken = deviceToken;
}

Swift:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    ThingSmartSDK.sharedInstance().deviceToken = deviceToken
}

Receive push notifications

Receives remote push notifications by calling the delegate method didReceiveRemoteNotification.

Example

Objective-C:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

}

Swift:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
}

Send push notifications

After you integrate with the capabilities of push notifications, you can configure push notifications on the Tuya Developer Platform.

Create an operation push notification

To create a marketing push notification, log in to the Tuya Developer Platform and go to the Marketing push page. Then, users can receive your in-app message regarding new arrivals, trending information, and more. This can help to build user engagement.

Integrate with Push Notifications

Create a device push notification

To create a device push notification, log in to the Tuya Developer Platform, and in the left-side navigation pane, choose Product > Notifications. For more information, see Configure Push Notification.

Integrate with Push Notifications