Suggestions

close search

Push notification

When your app becomes disconnected from Stringee server, you can not receive real-time udpates. In this case, you use Stringee Push Notification to keep your app's data updated.

Check iOS Push Notification to see how to set up Push Notification with Stringee.

Stringee use Remote Push notification to send push to devices for chat. So, you need to do the following steps:

1. Get token for remote push notification.

Call the following function in didFinishLaunchingWithOptions function to register device for receiving remote push notification from APNS:

- (void)remotePushRegistration {
    [UNUserNotificationCenter currentNotificationCenter].delegate = self;
    UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
        NSLog(@"XIN QUYEN REMOTE PUSH %d", (int)granted);
    }];

    [[UIApplication sharedApplication] registerForRemoteNotifications];
}

2. Register token with Stringee server

Send your device token to Stringee server as follow:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token = [self stringFromDeviceToken:deviceToken];
    NSLog(@"**************** Token - Remote push: %@", token);

    [[StringeeImplement instance].stringeeClient registerPushForDeviceToken:token isProduction:false isVoip:false completionHandler:^(BOOL status, int code, NSString *message) {
        NSLog(@"======= REGISTER REMOTE PUSH: %@", message);
    }];
}

Note:


- (NSString *)stringFromDeviceToken:(NSData *)deviceToken {
    NSUInteger length = deviceToken.length;
    if (length == 0) {
        return nil;
    }
    const unsigned char *buffer = deviceToken.bytes;
    NSMutableString *hexString  = [NSMutableString stringWithCapacity:(length * 2)];
    for (int i = 0; i < length; ++i) {
        [hexString appendFormat:@"%02x", buffer[i]];
    }
    return [hexString copy];
}

If everything is success, you will receive remote push notifications for chat. Stringee push notification data has the following format:

{
    data = {
        map = {
            data = {
                map = {
                    avatarUrl = "";
                    convId = "conv-vn-1-OZQMBPBUWM-1553965638358";
                    convName = Demo;
                    createdTime = 1554261740264;
                    displayName = hoangnam;
                    from = user1;
                    isGroup = 0;
                    message = {
                        map = {
                            content = Alo;
                            metadata = {
                                map = {};
                            };
                        };

                    };
                    msgId = "msg-vn-1-OZQMBPBUWM-1553965654245";
                    seq = 2;
                    type = 1;
                };
            };
            stringeePushNotification = "1.0";
            type = "CHAT_EVENT";
        };
    };
}