Configure push notifications

Updated

Configure push translations

You can use extension fields to implement customized push settings. This page uses force push, sending silent messages, and rich text push as examples to explain how to implement push extensions. For more information, see Offline push notification extension.

Set custom push fields

When creating a push message, you can add custom fields to the message to meet personalized business needs:

AgoraChatTextMessageBody *body = [[AgoraChatTextMessageBody alloc] initWithText:@"test"];
    NSString* currentUsername = AgoraChatClient.sharedClient.currentUsername;
    NSString* conversationId = @"remoteId";
    AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:conversationId from:currentUsername to:conversationId body:body ext:nil];
    message.ext = @{@"em_apns_ext":@{@"extern":@{@"test":123}}};
    message.chatType = AgoraChatTypeChat;
    [AgoraChatClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];

The data structure of the custom field is as follows:

{
    "em_apns_ext": {
      "extern": {"test": 123}
    }
}
ParametersDescription
em_apns_extThe built-in message extension fields.
externThe user-added custom key; multiple keys can be added.

Custom ringtones

When creating a push message, you can customize the notification tone when the recipient receives the message. Add the audio file to the app and configure its name used in the push. For details, see Apple documentation.

AgoraChatTextMessageBody *body = [[AgoraChatTextMessageBody alloc] initWithText:@"test"];
    AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:conversationId from:AgoraChatClient.sharedClient.currentUsername to:conversationId body:body ext:nil];
    message.ext = @{@"em_apns_ext":@{@"em_push_sound":@"custom.caf"}};
    message.chatType = AgoraChatTypeChat;
    [AgoraChatClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];

The data structure of the custom ringtone field is as follows:

{
  "ext": {
    "em_apns_ext": {
      "em_push_sound":"custom.caf"
    }
  }
}
ParametersDescription
em_apns_extThe built-in message extension fields.
em_push_soundThe key of the custom reminder ringtone field. This field is a built-in field and cannot be modified.
custom.cafThe name of the audio file for the ringtone.

Force push

The user can set force push to ignore the recipient's DND setting when sending messages:

AgoraChatTextMessageBody *body = [[AgoraChatTextMessageBody alloc] initWithText:@"test"];
AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:conversationId from:AgoraChatClient.sharedClient.currentUsername to:conversationId body:body ext:nil];
// Set whether to force push. This extension field is a built-in field with the following values: ` YES ` : force push; (default) ` NO ` : non-force push.
message.ext = @{@"em_force_notification":@YES};
message.chatType = AgoraChatTypeChat;
[AgoraChatClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];

Send a silent message

Sending silent messages means that the sender sets the message not to be pushed when sending it. That is, when the user is offline, Chat will not push message notifications to the user's device through the third-party push service. When the user is online again, they will receive all the messages received during the offline period.

Both silent message sending and do not disturb mode pause push messages. The difference is that silent message sending is set by the sender when sending the message, while the DND mode is set by the receiver.

AgoraChatTextMessageBody *body = [[AgoraChatTextMessageBody alloc] initWithText:@"test"];
AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:conversationId from:AgoraChatClient.sharedClient.currentUsername to:conversationId body:body ext:nil];
// Set whether to send silent messages. This field is a built-in extension field with the following values: `YES`: send silent messages; (default) `NO`: push the message.
message.ext = @{@"em_ignore_notification":@YES};
message.chatType = AgoraChatTypeChat;
[AgoraChatClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];

Implement rich text push

If your target platform is iOS 10.0 or above, you can refer to the following code to implement the rich text push function of UNNotificationServiceExtension.

AgoraChatTextMessageBody *body = [[AgoraChatTextMessageBody alloc] initWithText:@"test"];
AgoraChatMessage *message = [[AgoraChatMessage alloc] initWithConversationID:conversationId from:AgoraChatClient.sharedClient.currentUsername to:conversationId body:body ext:nil];
// em_apns_ext: message extension field
message.ext = @{@"em_apns_ext":@{@"em_push_mutable_content":@YES}};
message.chatType = AgoraChatTypeChat;
[AgoraChatClient.sharedClient.chatManager sendMessage:message progress:nil completion:nil];
ParametersDescription
bodyThe push message content.
conversationIdThe conversation ID to which the message belongs.
fromThe user ID of the sender of the message.
toThe user ID of the message recipient.
em_apns_extThe built-in message extension fields.
em_push_mutable_contentWhether to use rich text push notification (em_apns_ext): YES: Rich text push notification; (Default)NO: Regular push notification. This field is a built-in field and its name cannot be modified.

When the receiver gets a rich text push, the didReceiveNotificationRequest:withContentHandler: callback is triggered. The sample code is as follows:

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
    // Push extension fields
    NSDictionary *userInfo = request.content.userInfo;
    // Notification content
    UNNotificationContent *content = [request.content mutableCopy];
    contentHandler(content);
}
ParametersDescription
bodyThe push message content. .
badgeThe square badge number.
soundThe recipient will hear a ringtone when receiving the message.
mutable-contentSet to 1 to activate UNNotificationServiceExtension .
fThe user ID of the message sender.
tThe user ID of the message recipient.
mThe message ID.