NTESCustomSysNotificationSender.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // NTESCustomSysNotiSender.m
  3. // NIM
  4. //
  5. // Created by chris on 15/5/26.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "NTESCustomSysNotificationSender.h"
  9. #import "NIMKitInfoFetchOption.h"
  10. @interface NTESCustomSysNotificationSender ()
  11. @property (nonatomic,strong) NSDate *lastTime;
  12. @end
  13. @implementation NTESCustomSysNotificationSender
  14. - (void)sendCustomContent:(NSString *)content toSession:(NIMSession *)session{
  15. if (!content) {
  16. return;
  17. }
  18. NSDictionary *dict = @{
  19. NTESNotifyID : @(NTESCustom),
  20. NTESCustomContent : content,
  21. };
  22. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  23. options:0
  24. error:nil];
  25. NSString *json = [[NSString alloc] initWithData:data
  26. encoding:NSUTF8StringEncoding];
  27. NIMCustomSystemNotification *notification = [[NIMCustomSystemNotification alloc] initWithContent:json];
  28. notification.apnsContent = content;
  29. notification.sendToOnlineUsersOnly = NO;
  30. NIMCustomSystemNotificationSetting *setting = [[NIMCustomSystemNotificationSetting alloc] init];
  31. setting.apnsEnabled = YES;
  32. notification.setting = setting;
  33. [[[NIMSDK sharedSDK] systemNotificationManager] sendCustomNotification:notification
  34. toSession:session
  35. completion:nil];
  36. }
  37. - (void)sendTypingState:(NIMSession *)session
  38. {
  39. NSString *currentAccount = [[[NIMSDK sharedSDK] loginManager] currentAccount];
  40. if (session.sessionType != NIMSessionTypeP2P ||
  41. [session.sessionId isEqualToString:currentAccount])
  42. {
  43. return;
  44. }
  45. NSDate *now = [NSDate date];
  46. if (_lastTime == nil ||
  47. [now timeIntervalSinceDate:_lastTime] > 3)
  48. {
  49. _lastTime = now;
  50. NSDictionary *dict = @{NTESNotifyID : @(NTESCommandTyping)};
  51. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  52. options:0
  53. error:nil];
  54. NSString *content = [[NSString alloc] initWithData:data
  55. encoding:NSUTF8StringEncoding];
  56. NIMCustomSystemNotification *notification = [[NIMCustomSystemNotification alloc] initWithContent:content];
  57. notification.sendToOnlineUsersOnly = YES;
  58. NIMCustomSystemNotificationSetting *setting = [[NIMCustomSystemNotificationSetting alloc] init];
  59. setting.apnsEnabled = NO;
  60. notification.setting = setting;
  61. [[[NIMSDK sharedSDK] systemNotificationManager] sendCustomNotification:notification
  62. toSession:session
  63. completion:nil];
  64. }
  65. }
  66. - (void)sendCallNotification:(NSString *)teamId
  67. roomName:(NSString *)roomName
  68. members:(NSArray *)members
  69. {
  70. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:teamId];
  71. NSDictionary *dict = @{
  72. NTESNotifyID : @(NTESTeamMeetingCall),
  73. NTESTeamMeetingMembers : members,
  74. NTESTeamMeetingTeamId : teamId,
  75. NTESTeamMeetingTeamName : team.teamName? team.teamName : @"群组",
  76. NTESTeamMeetingName : roomName
  77. };
  78. NSData *data = [NSJSONSerialization dataWithJSONObject:dict
  79. options:0
  80. error:nil];
  81. NSString *content = [[NSString alloc] initWithData:data
  82. encoding:NSUTF8StringEncoding];
  83. NIMCustomSystemNotification *notification = [[NIMCustomSystemNotification alloc] initWithContent:content];
  84. notification.sendToOnlineUsersOnly = NO;
  85. NIMKitInfoFetchOption *option = [[NIMKitInfoFetchOption alloc] init];
  86. option.session = [NIMSession session:teamId type:NIMSessionTypeTeam];
  87. NIMKitInfo *me = [[NIMKit sharedKit] infoByUser:[NIMSDK sharedSDK].loginManager.currentAccount option:option];
  88. notification.apnsContent = [NSString stringWithFormat:@"%@正在呼叫您",me.showName];
  89. NIMCustomSystemNotificationSetting *setting = [[NIMCustomSystemNotificationSetting alloc] init];
  90. setting.apnsEnabled = YES;
  91. notification.setting = setting;
  92. for (NSString *userId in members) {
  93. if ([userId isEqualToString:[NIMSDK sharedSDK].loginManager.currentAccount])
  94. {
  95. continue;
  96. }
  97. NIMSession *session = [NIMSession session:userId type:NIMSessionTypeP2P];
  98. [[NIMSDK sharedSDK].systemNotificationManager sendCustomNotification:notification toSession:session completion:nil];
  99. }
  100. }
  101. @end