1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.mobileNotification;
37 import java.util.HashSet;
38 import java.util.List;
41 import javapns.communication.exceptions.CommunicationException;
42 import javapns.communication.exceptions.KeystoreException;
44 import com.liferay.portal.kernel.dao.orm.QueryUtil;
45 import com.liferay.portal.kernel.exception.PortalException;
46 import com.liferay.portal.kernel.exception.SystemException;
47 import com.liferay.portal.kernel.util.OrderByComparator;
48 import com.liferay.portal.kernel.workflow.WorkflowConstants;
49 import com.liferay.portal.model.Group;
50 import com.liferay.portal.model.Organization;
51 import com.liferay.portal.model.User;
52 import com.liferay.portal.service.ExtUserLocalServiceUtil;
53 import com.liferay.portal.service.GroupLocalServiceUtil;
54 import com.liferay.portal.service.OrganizationLocalServiceUtil;
55 import com.liferay.portal.service.UserLocalServiceUtil;
56 import com.liferay.portal.util.comparator.UserLastNameComparator;
57 import com.pentila.entSavoie.mobileNotification.androidUtils.AndroidPushService;
58 import com.pentila.entSavoie.userProperties.model.UserMobileNotification;
59 import com.pentila.entSavoie.userProperties.service.UserMobileNotificationLocalServiceUtil;
61 public class MobileNotification {
63 public static void sendMobileNotification(long userId, String message) throws PortalException, SystemException, CommunicationException, KeystoreException {
65 List<UserMobileNotification> userMobNot = UserMobileNotificationLocalServiceUtil.getUserMobileNotificationByUserIdEnable(userId, true);
67 for (UserMobileNotification umn : userMobNot) {
68 if (umn.getDevice().equals("ios")) {
69 Push.combined(message, 0, "default", "/home/liferay/NeroApps.p12", "pentila", false, umn.getToken());
72 AndroidPushService aps = new AndroidPushService();
73 aps.sendNotification(message, umn.getToken());
79 public static void sendMobileNotificationGroup(List<Long> groupIds, long senderId, String message) throws PortalException, SystemException {
81 // on prends toutes les personnes du groupe
82 User sender = UserLocalServiceUtil.getUser(senderId);
83 HashSet<User> users = new HashSet<User>();
84 OrderByComparator odb = new UserLastNameComparator(true);
85 for (Long groupId: groupIds) {
86 Group grp = GroupLocalServiceUtil.getGroup(groupId);
87 if (grp.isOrganization()) {
89 final Organization org = OrganizationLocalServiceUtil.getOrganization(grp.getOrganizationId());
90 users.addAll(ExtUserLocalServiceUtil.getOrganizationUsers(org.getOrganizationId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS, odb));
94 final java.util.LinkedHashMap<String, Object> userParams = new java.util.LinkedHashMap<String, Object>();
95 userParams.put("usersGroups", groupId);
96 users.addAll(UserLocalServiceUtil.search(sender.getCompanyId(), "", WorkflowConstants.STATUS_APPROVED, userParams, QueryUtil.ALL_POS, QueryUtil.ALL_POS, odb));
101 for (User u: users) {
103 // on n'envoie pas au sender!!!
104 if (u.getUserId()==senderId) {
107 MobileNotification.sendMobileNotification(u.getUserId(), message);
108 } catch (Exception e) {