System Notification

Update time: 2021/09/25 14:33:49

In addition to the message channel, SDK also provides a system notification channel for distribution of notifications other than messages. There are currently two types: built-in system notification and custom system notification. System notifications mainly include team change notifications, such as application for joining team and team invitation. If the third-party app also hosts friendship, addition and removal of friends are also included into this type of notification. System notifications are received and stored by SDK that supports simple management of unread count. Push parameter settings are available when sending custom system notifications.

Note: The notifications of team joining application and invitation are different from the notifications of team operation results, such as those of creating a team, dismissing a team, removing members, inviting people into the team, etc. Notifications for team operations are delivered through the IM message channel (see team notification event for details).

NIMSysMessageParameter description

Type Parameter Description
ResponseCode Response Notification error codes, please refer to ResponseCodefor details
NIMMessageFeature Feature Notification attributes, with differences between offline, multi-terminal synchronization, roaming notifications, etc., see NIMMessageFeature for details
int TotalUnread Total number of unread notifications
NIMSysMessageContent Content For notification content, see NIMSysMessageContent.

NIMSysMsgTypeDescription of notification type

Enumeration Value Description
kNIMSysMsgTypeTeamApply 0 Apply for joining team
kNIMSysMsgTypeTeamReject 1 Reject joining application
kNIMSysMsgTypeTeamInvite 2 Invite to team
kNIMSysMsgTypeTeamInviteReject 3 Reject invitation to join
kNIMSysMsgTypeFriendAdd 5 Add friend
kNIMSysMsgTypeFriendDel 6 Delete friend
kNIMSysMsgTypeCustomP2PMsg 100 Transparent transmission of p2p messages
kNIMSysMsgTypeCustomTeamMsg 101 Transparent transmission of team messages
kNIMSysMsgTypeUnknown 1000 Unknown type, as default

NIMSysMessageContentParameter description

Type Parameter Description
long Timetag Timestamp, optional
NIMSysMsgType MsgType Type of notification, such as team joining application, team joining invitation, add friend, etc., see NIMSysMsgType for details
string ReceiverId Receiver id, user id if receiver is a user, or team id if receiver is a team, required
string SenderId Sender id, optional in cases such as sending a custom notification
string Message Notification P.S., to be completed as needed
string Attachment Attachment, to be completed as needed, similar to the attachment content of ordinary IM messages, view the message sent
long Id Server message id (custom notification, value filled in must be 0), not required for the sender
int SupportOffline (Optional) Whether to enable offline save of the notification message: 0-Only send it to online users, 1- It can be sent to offline users
string PushContent Push notification copy of custom system notification, no push if left blank
NIMSysMsgStatus Status Locally defined system message status, refer to NIMSysMsgStatus, not required for the sender
int NeedPush (Optional) Whether push is needed, 0: not needed, 1: needed, set to 1 by default, should be used withPushContent
int NeedPushCount (Optional) Whether message count (corner mark) is needed, 0: not needed, 1: needed, set to 1 by default, should be used withPushContent
int NeedPushPrefix (Optional) Whether nickname push is needed, 0: not needed, 1: needed, set to 0 by default, should be used withNeedPush
JsonExtension CustomPushContent (Optional) Third-party custom push load content, must be an unformatted string that can be parsed as json, with a length of 2048
bool AntiSpamEnabled (Optional) Whether to enable anti-spam of YiDun, set to false by default
string AntiSpamContent (Optional) Developer-defined anti-spam field, with a length limit of 5000 characters, should be used withAntiSpamEnabled

Message settings such as push, anti-spam, etc. similar to IM message setting.

Receive system notification

Register/cancel the event of receiving system messages. Registration needs to be completed before logging in. To ensure consistent logic of the entire program, APP needs to perform operations correspondingly with different types of system notifications.

  • API prototype
public static EventHandler<NIMSysMsgEventArgs> ReceiveSysMsgHandler;
  • Parameter description, not available

  • Example

//Callback
void OnReceiveSysMessage(object sender, NIMSysMsgEventArgs e)
{
    //Process notification message
    ...
}
//Monitor event
NIM.SysMessage.SysMsgAPI.ReceiveSysMsgHandler += OnReceiveSysMessage;

//Cancel monitoring
NIM.SysMessage.SysMsgAPI.ReceiveSysMsgHandler -= OnReceiveSysMessage;

Send custom system notification

In addition to built-in system notification, SDK also provides a custom system for developers to facilitate the notification of business logic. The notification may be issued by either the client or the developer server.

The format of custom notification sent from client is defined by the developer (Attachment), and SDK is only responsible for sending and receiving the notification via online or offline channel, including point-to-point notification and team notification, but without doing any parsing or storage. Therefore, such notification will not be reflected in chat history, and it is available to scenes such as sending the status of typing, etc.

In addition, the content of custom system notification (NIMSysMessageContent) also provides attribute settings as follows:

  • Whether custom notification is saved offline
  • Push notification copy of custom system notification
  • Third-party custom push attribute
  • Determine if push is needed
  • Whether counting is needed for push message
  • Whether a nickname is needed for push message

SDK is not responsible for the persistence of custom notifications, parsing and persisting of which should be done by APP as needed according to its own business logic.

  • API prototype
 public static void SendCustomMessage(NIMSysMessageContent content);
  • Parameter Description
Parameter Description
content For message content, see NIMSysMessageContent.
  • Example
//Take the test account test1 as an example;
NIMSysMessageContent content = new NIMSysMessageContent();

content.ReceiverId = "test1";
content.MsgType = NIMSysMsgType.kNIMSysMsgTypeCustomP2PMsg;

JsonExtension attach = new JsonExtension();
attach.AddItem("id", "2");
attach.AddItem("content", "attachment");
content.Attachment = attach.Value;

content.Timetag = 1520423612222;//Current time UNIX timestamp, 13-byte (ms)

//Offline storage
content.SupportOffline = NIMMessageSettingStatus.kNIMMessageStatusSetted;

//Push and count
content.NeedPush = NIMMessageSettingStatus.kNIMMessageStatusSetted;
content.NeedPushCount = NIMMessageSettingStatus.kNIMMessageStatusSetted;

//Push notification with prefix display, e.g. mmm: notification content
content.NeedPushPrefix = NIMMessageSettingStatus.kNIMMessageStatusNotSet;

//Content of push message
content.PushContent = "This is a push notification";
//You can add payload information push
JsonExtension push_content = new JsonExtension();
push_content.AddItem("Content", "This is to push load information");
content.CustomPushContent = push_content;


content.ClientMsgId = "uuid";//Need to generate unique uuid

....

//Send message
NIM.SysMessage.SysMsgAPI.SendCustomMessage(content);

Result notification of sending custom system notification

Result notification of sending custom notification message.

  • API prototype
public static EventHandler<NIMSysMsgEventArgs> SendSysMsgHandler;
  • Parameter description, not available

  • Example

//Callback
void OnSendSysMsgHandler(object sender, NIMSysMsgEventArgs e)
{
    //Process notification message
    ...
}
//Monitor event
NIM.SysMessage.SysMsgAPI.SendSysMsgHandler += OnSendSysMsgHandler;

//Cancel monitoring
NIM.SysMessage.SysMsgAPI.SendSysMsgHandler -= OnSendSysMsgHandler;

Notification message management

SDK provides interfaces for querying message history, unread messages, setting as read, deleting notifications, etc.

Query system notification

Query local system messages (search in reverse order of time, with results sorted in reverse order).

NIMSysMsgQueryResultParameter description

Type Parameter Description
int count Total number of messages
int UnreadCount Total number of unread notification messages
NIMSysMessageContent[] MsgCollection For set of notification messages, see NIMSysMessageContent.
  • API prototype
 public static void QueryMessage(int limit, long lastTimetag, QuerySysMsgResult cb);
  • Parameter Description
Parameter Description
limit Number of sessions for one query, 20 is the recommended value
lastTimetag The timestamp of the last message of the last query (searched in reverse order of time, meaning it is the smallest timestamp), fill in 0 for initial query
cb Operation result callback
  • Example
//Initial query
NIM.SysMessage.SysMsgAPI.QueryMessage(20,0,(result)=>{
	...
});

//Assuming the last message timestamp of the initial query is '1520423612222’
NIM.SysMessage.SysMsgAPI.QueryMessage(20,1520423612222,(result)=>{
	...
});

Query unread count

  • API prototype
  public static void QueryUnreadCount(CommomOperateResult cb);
  • Parameter Description
Parameter Description
cb Operation result callback
  • Example
// Query unread count
NIM.SysMessage.SysMsgAPI.QueryUnreadCount((code, count)=>{
	...
});

Set message status

Message status settings include Pass, Decline, Read, Invalid, etc.

NIMSysMsgStatusEnumeration description

Enumeration Value Description
kNIMSysMsgStatusNone 0 Default, unread
kNIMSysMsgStatusPass 1 Received, pass
kNIMSysMsgStatusDecline 2 Received, decline
kNIMSysMsgStatusRead 3 Received, read
kNIMSysMsgStatusDeleted 4 Deleted
kNIMSysMsgStatusInvalid 5 Invalid
  • API prototype
 public static void SetMsgStatus(long msgId, NIMSysMsgStatus status, OperateSysMsgExternDelegate cb);
  • Parameter Description
Parameter Description
msgId Message id
status Message status, see NIMSysMsgStatus for details
cb Operation result callback
  • Example
// Set message status:
long msgId = 213213111;
NIM.SysMessage.SysMsgAPI.SetMsgStatus(msgId,NIMSysMsgStatus.kNIMSysMsgStatusPass,(code, msg_id, unread_count, json_extension, user_data)=>{
	if (code == 200)//success
    {
		...
    }
});

Set message status in batches by message type

  • API prototype
 public static void SetMsgStatusByType(NIMSysMsgType type, NIMSysMsgStatus status, OperateSysMsgDelegate cb);
  • Parameter Description
Parameter Description
type Message type, see NIMSysMsgType for details
status Message status, see NIMSysMsgStatus for details
cb Operation result callback
  • Example
//Set message status by type
NIM.SysMessage.SysMsgAPI.SetMsgStatusByType(NIMSysMsgType.kNIMSysMsgTypeTeamApply,NIMSysMsgStatus.kNIMSysMsgStatusPass,(code, unread_count, json_extension, user_data)=>{
	if (code == 200)//success
    {
		...
    }
});

Set all read

  • API prototype
  public static void SetAllMsgRead(OperateSysMsgDelegate cb);
  • Parameter Description
Parameter Description
cb Operation result callback
  • Example
//Set all read
NIM.SysMessage.SysMsgAPI.SetAllMsgRead((code, unread_count, json_extension, user_data)=>{
	if (code == 200)//success
    {
		...
    }
});

Delete a single notification

  • API prototype
  public static void DeleteByMsgId(long msgId, OperateSysMsgExternDelegate cb);
  • Parameter Description
Parameter Description
msgId Message id
cb Operation result callback
  • Example
// Delete messages
long msgId = 21321311;
NIM.SysMessage.SysMsgAPI.DeleteByMsgId(msgId,(code, msgId,unread_count, json_extension, user_data)=>{
	if (code == 200)//success
    {
		...
    }
});

Delete messages by type in batches

  • API prototype
  public static void DeleteMsgByType(NIMSysMsgType type, OperateSysMsgDelegate cb);
  • Parameter Description
Parameter Description
type Message type, see NIMSysMsgType for details
cb Operation result callback
  • Example
//Delete all messages of the type kNIMSysMsgTypeTeamApply
NIM.SysMessage.SysMsgAPI.DeleteMsgByType(NIMSysMsgType.kNIMSysMsgTypeTeamApply,(code, unread_count, json_extension, user_data)=>{
	if (code == 200)//success
    {
		...
    }
});
Was this page helpful?
Yes
No
  • Receive system notification
  • Send custom system notification
  • Result notification of sending custom system notification
  • Notification message management
  • Query system notification
  • Query unread count
  • Set message status
  • Set message status in batches by message type
  • Set all read
  • Delete a single notification
  • Delete messages by type in batches