To receive APNs offline message notifications, complete the following steps:
When the app configured with APNs switches to the background or is killed by the user, the Tencent Cloud backend pushes offline messages to the device through Apple’s APNs. For more information, see Apple Push Notification Service.
Note:
Users who have logged out normally or forcibly will not receive any message notifications.
For details on how to apply for an APNs certificate, see Applying for an Apple Push Certificate.
Note:
- We recommend that the name of the certificate to be uploaded is completely in English letters (and must not contain special characters such as brackets).
- You need to set a password for the uploaded certificate. Without a password, push messages cannot be received.
- Certificates to be published in App Store need to be set to the Release environment. Otherwise, push notifications cannot be received.
- The uploaded p12 certificate must be an authentic and valid certificate that you have personally applied for.
To request a deviceToken from Apple’s backend server, add the following code to your app.
// Request a deviceToken from Apple’s backend.
- (void)registNotification
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
}
}
//The callback of AppDelegate returns a deviceToken, which needs to be reported to the Tencent Cloud backend after login.
-(void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//Take note of the deviceToken returned by Apple.
_deviceToken = deviceToken;
}
After logging in to the IM SDK, call setAPNS to uploaded the deviceToken obtained in step 3 to the Tencent Cloud backend:
V2TIMAPNSConfig *confg = [[V2TIMAPNSConfig alloc] init];
// Business certificate ID, which is generated after the certificate is uploaded to the IM console
confg.businessID = businessID;
// Requested deviceToken from Apple’s backend
confg.token = deviceToken;
[[V2TIMManager sharedInstance] setAPNS:confg succ:^{
NSLog(@"-----> Set APNs successfully");;
} fail:^(int code, NSString *msg) {
NSLog(@"-----> Failed to set APNs");
}];
Note:
The businessID must be consistent with the certificate ID assigned by the console.
The following gives an example of the push format.
For one-to-one chat messages, the APNs push rules are as follows. Note that the nickname is the sender’s nickname. If the nickname is not specified, only the content is displayed.
Nickname: content
For group chat messages, the APNs push rules are as follows. The priority of displayed names is: the message sender’s group name card
> group nickname
. If neither of these two items is available, no name is displayed.
Name (group name): content
APNs push content consists of the content of each Elem
in the message body. The display of different Elem
items in offline messages is described in the following table.
Parameter | Description |
---|---|
Text Elem | Directly display the content |
Audio Elem | Display [audio] |
File Elem | Display [file] |
Image Elem | Display [image] |
Custom Elem | Display the desc field set when the message was sent. If desc was not specified, the message will not be pushed. |
If you set SDKAppID
to the same value for multiple apps, these apps can communicate with each other. Different apps need to use different push certificates, and you need to apply for an APNs certificate for each app and complete [offline push configuration](#configuring push).
Set the iOSSound
field of offlinePushInfo when calling sendMessage to send messages. iOSSound
passes the name (with the extension) of the audio file that must be linked to the Xcode project.
Set the title
and desc
fields of offlinePushInfo when calling sendMessage to send messages. Once set, the title
content will be added to the default push content, and desc
will be displayed as the push content.
Set the ext
field of offlinePushInfo when calling sendMessage to send messages. When the user receives an offline push notification and starts the app, the ext
field can be obtained from the AppDelegate -> didReceiveRemoteNotification
system callback, and the user is redirected to the UI as specified by ext
.
The following example assumes that Denny sends a message to Vinson.
ext
before sending a message.// Denny sets `offlinePushInfo` and specifies `ext` before sending a message.
V2TIMMessage *msg = [[V2TIMManager sharedInstance] createTextMessage:@"Text message"];
V2TIMOfflinePushInfo *info = [[V2TIMOfflinePushInfo alloc] init];
info.ext = @"jump to denny";
[[V2TIMManager sharedInstance] sendMessage:msg receiver:@"vinson" groupID:nil priority:V2TIM_PRIORITY_DEFAULT
onlineUserOnly:NO offlinePushInfo:info progress:^(uint32_t progress) {
} succ:^{
} fail:^(int code, NSString *msg) {
}];
Recipient: although Vinson's app is not online, it can still receive an APNs offline message notification. When Vinson clicks this notification, the app is started.
// Vinson receives the following callback when the app is started.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// Parse the desc extension field.
if ([userInfo[@"ext"] isEqualToString:@"jump to denny"]) {
// Jump to the chat window with Denny.
}
}
First, verify that the app runtime environment is consistent with that of the certificate. Otherwise, offline push notifications cannot be received.
Then, verify that the app and the certificate are in the development environment. If this is the case, the request for deviceToken
from Apple might fail. To solve this problem, switch to the production environment.
The offline push feature for custom messages is different from ordinary messages. Since we are not able to resolve the content of custom messages and determine what to push, we simply do not push custom messages by default. If you do need to enable offline push for custom messages, set the desc
field of offlinePushInfo when calling sendMessage. Then, the content of desc
will be used for display during offline push.
Was this page helpful?