コード例 #1
0
        public async Task PushNotifyPostMatch(string userId, NotificationDetail notificationDetail)
        {
            var currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            await AddToGroup(
                new AddUserToGroupRequest
            {
                GroupName = userId,
                Type      = Feature.GetTypeName(currentUser),
                UserIds   = new List <string> {
                    userId
                }
            }
                );

            var finder = Builders <ClientGroup> .Filter.Eq("name", userId);

            var clientGroup = await clientGroupRepository.FindAsync(finder);

            foreach (string id in clientGroup.UserIds)
            {
                NotificationDetail finalNotificationDetail = new NotificationDetail()
                {
                    CreatorId            = notificationDetail.CreatorId,
                    ReceiverId           = userId,
                    NotificationObjectId = notificationDetail.NotificationObjectId
                };
                await notificationDetailRepository.AddAsync(finalNotificationDetail);

                NotificationObject notificationObject =
                    await notificationObjectRepository.GetByIdAsync(
                        ObjectId.Parse(finalNotificationDetail.NotificationObjectId));

                FilterDefinition <NotificationType> notificationTypeFilter =
                    Builders <NotificationType> .Filter.Eq("code", notificationObject.NotificationType);

                NotificationType notificationType = await notificationTypeRepository.FindAsync(notificationTypeFilter);

                User creator = await userRepository.GetByIdAsync(ObjectId.Parse(finalNotificationDetail.CreatorId));

                PushedNotificationViewModel notificationToPush = new PushedNotificationViewModel()
                {
                    AuthorName   = $"{creator.FirstName} {creator.LastName}",
                    AuthorAvatar = creator.AvatarHash,
                    AuthorId     = creator.OId,
                    Content      = $"{notificationType.ContentTemplate}.",
                    CreatedDate  = DateTime.Now,
                    IsRead       = false,
                    ModifiedDate = DateTime.Now,
                    ObjectId     = notificationObject.ObjectId,
                    OId          = notificationObject.OId,
                    OwnerId      = userId,
                    Status       = ItemStatus.Active
                };
                //Push notification
                try
                {
                    FilterDefinition <FcmInfo> userFilter = Builders <FcmInfo> .Filter.Eq("user_id", userId);

                    string token = (await fcmInfoRepository.FindAsync(userFilter)).DeviceToken;

                    FirebaseAdmin.Messaging.Message mes = new FirebaseAdmin.Messaging.Message()
                    {
                        Token = token,
                        Data  = new Dictionary <string, string>()
                        {
                            { "notification", JsonConvert.SerializeObject(notificationToPush) }
                        },
                        Notification = new Notification()
                        {
                            Title    = creator.LastName,
                            Body     = notificationToPush.Content,
                            ImageUrl = creator.AvatarHash
                        }
                    };
                    string response = await FirebaseMessaging.DefaultInstance.SendAsync(mes).ConfigureAwait(true);
                }
                catch (Exception)
                {
                    // do nothing
                }
            }
        }
コード例 #2
0
        public async Task PushNotifyDetail(string clientGroupName, NotificationDetail notificationDetail)
        {
            FilterDefinition <ClientGroup> finder = Builders <ClientGroup> .Filter.Eq("name", clientGroupName);

            ClientGroup clientGroup = await clientGroupRepository.FindAsync(finder);

            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            foreach (string receiverId in clientGroup.UserIds)
            {
                var existNotificationDetailBuilder = Builders <NotificationDetail> .Filter;
                var existNotificationDetailFilter  =
                    existNotificationDetailBuilder.Eq("creator_id", notificationDetail.CreatorId)
                    & existNotificationDetailBuilder.Eq("receiver_id", receiverId)
                    & existNotificationDetailBuilder.Eq("notification_object_id",
                                                        notificationDetail.NotificationObjectId);

                var existNotificationDetail =
                    await notificationDetailRepository.FindAsync(existNotificationDetailFilter);

                var finalNotificationDetail = new NotificationDetail();

                if (existNotificationDetail != null)
                {
                    finalNotificationDetail = existNotificationDetail;
                    finalNotificationDetail.ModifiedDate = DateTime.Now;
                    finalNotificationDetail.IsDeleted    = false;
                    await notificationDetailRepository.UpdateAsync(finalNotificationDetail, finalNotificationDetail.Id);
                }
                else
                {
                    finalNotificationDetail.CreatorId            = notificationDetail.CreatorId;
                    finalNotificationDetail.ReceiverId           = receiverId;
                    finalNotificationDetail.NotificationObjectId = notificationDetail.NotificationObjectId;
                    await notificationDetailRepository.AddAsync(finalNotificationDetail);
                }

                ;

                var notificationObject =
                    await notificationObjectRepository.GetByIdAsync(
                        ObjectId.Parse(finalNotificationDetail.NotificationObjectId));

                var owner = await userRepository.GetByIdAsync(ObjectId.Parse(notificationObject.OwnerId));

                var receiver = await userRepository.GetByIdAsync(ObjectId.Parse(receiverId));

                var creator = await userRepository.GetByIdAsync(ObjectId.Parse(finalNotificationDetail.CreatorId));

                var notificationTypeFilter =
                    Builders <NotificationType> .Filter.Eq("code", notificationObject.NotificationType);

                var notificationType = await notificationTypeRepository.FindAsync(notificationTypeFilter);

                string notifyContent = string.Empty;

                if (owner.OId == creator.OId)
                {
                    if (receiver.OId == owner.OId)
                    {
                        notifyContent = $"Bạn {notificationType.ContentTemplate} chính mình";
                    }
                    else if (receiver.OId != owner.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} họ";
                    }
                }
                else if (owner.OId != creator.OId)
                {
                    if (receiver.OId != owner.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} {owner.LastName}";
                    }
                    else if (receiver.OId != creator.OId)
                    {
                        notifyContent = $"{creator.LastName} {notificationType.ContentTemplate} bạn";
                    }
                }


                var notificationToPush = new PushedNotificationViewModel()
                {
                    AuthorName   = $"{creator.FirstName} {creator.LastName}",
                    AuthorAvatar = creator.AvatarHash,
                    AuthorId     = creator.OId,
                    Content      = notifyContent,
                    CreatedDate  = DateTime.Now,
                    IsRead       = false,
                    ModifiedDate = DateTime.Now,
                    ObjectId     = notificationObject.ObjectId,
                    OId          = notificationObject.OId,
                    OwnerId      = owner.OId,
                    Status       = ItemStatus.Active
                };

                switch (notificationType.Code)
                {
                case "ADD_POST_NOTIFY":
                case "UPVOTE_POST_NOTIFY":
                case "DOWNVOTE_POST_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Post;
                    break;

                case "UPVOTE_COMMENT_NOTIFY":
                case "DOWNVOTE_COMMENT_NOTIFY":
                case "COMMENT_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Comment;
                    break;

                case "UPVOTE_REPLY_NOTIFY":
                case "DOWNVOTE_REPLY_NOTIFY":
                case "REPLY_COMMENT_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.Reply;
                    break;

                case "FOLLOW_NOTIFY":
                    notificationToPush.NotificationType = PushedNotificationType.User;
                    break;

                default:
                    notificationToPush.NotificationType = PushedNotificationType.Other;
                    break;
                }

                try
                {
                    if (!(owner.OId == creator.OId && receiver.OId == owner.OId))
                    {
                        var userFilter = Builders <FcmInfo> .Filter.Eq("user_id", receiverId);

                        string token = (await fcmInfoRepository.FindAsync(userFilter)).DeviceToken;

                        FirebaseAdmin.Messaging.Message mes = new FirebaseAdmin.Messaging.Message()
                        {
                            Token = token,
                            Data  = new Dictionary <string, string>()
                            {
                                { "notification", JsonConvert.SerializeObject(notificationToPush) }
                            },
                            Notification = new Notification()
                            {
                                Title    = creator.LastName,
                                Body     = notifyContent,
                                ImageUrl = creator.AvatarHash
                            }
                        };
                        string response = await FirebaseMessaging.DefaultInstance.SendAsync(mes).ConfigureAwait(true);
                    }
                }
                catch (Exception)
                {
                    // do nothing
                }
            }
        }