Commit abf181bd02bc2848fd068416f4d662f97a6279f7
1 parent
50ca3597
Exists in
yxb_dev
and in
2 other branches
no message
Showing
9 changed files
with
359 additions
and
31 deletions
Show diff stats
.idea/modules.xml
... | ... | @@ -4,8 +4,8 @@ |
4 | 4 | <modules> |
5 | 5 | <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> |
6 | 6 | <module fileurl="file://$PROJECT_DIR$/mvpsdk/mvpsdk.iml" filepath="$PROJECT_DIR$/mvpsdk/mvpsdk.iml" /> |
7 | + <module fileurl="file://$PROJECT_DIR$/parentWorkHolper.iml" filepath="$PROJECT_DIR$/parentWorkHolper.iml" /> | |
7 | 8 | <module fileurl="file://E:\parentwork\parentWorkHolper.iml" filepath="E:\parentwork\parentWorkHolper.iml" /> |
8 | - <module fileurl="file://$PROJECT_DIR$/parentwork.iml" filepath="$PROJECT_DIR$/parentwork.iml" /> | |
9 | 9 | <module fileurl="file://F:\parentWorkHolper\parentwork.iml" filepath="F:\parentWorkHolper\parentwork.iml" /> |
10 | 10 | <module fileurl="file://$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" filepath="$PROJECT_DIR$/roundedimageview-2.2.1/roundedimageview-2.2.1.iml" /> |
11 | 11 | </modules> | ... | ... |
app/build.gradle
... | ... | @@ -47,4 +47,5 @@ dependencies { |
47 | 47 | implementation project(':roundedimageview-2.2.1') |
48 | 48 | implementation files('libs/AMap_Location_V3.8.0_20180201.jar') |
49 | 49 | implementation 'org.greenrobot:greendao:3.2.2' // add library |
50 | + compile "me.leolin:ShortcutBadger:1.1.19@aar" | |
50 | 51 | } | ... | ... |
app/src/main/AndroidManifest.xml
... | ... | @@ -65,11 +65,11 @@ |
65 | 65 | <!-- android:name="com.amap.api.v2.apikey" --> |
66 | 66 | <!-- android:value="0da7965006ae0c8e4106c76c1867d976"/> --> |
67 | 67 | |
68 | - <!--家长慧测试APPkey--> | |
68 | + | |
69 | + <!-- 家长慧测试APPkey --> | |
69 | 70 | <meta-data |
70 | 71 | android:name="com.netease.nim.appKey" |
71 | 72 | android:value="330158c080acdf4dc1092d6a74576c2c"/> |
72 | - | |
73 | 73 | <meta-data |
74 | 74 | android:name="android.max_aspect" |
75 | 75 | android:value="2.1"/> |
... | ... | @@ -112,7 +112,8 @@ |
112 | 112 | android:name="com.amap.api.v2.apikey" |
113 | 113 | android:value="1d130afb822d8a1019e6592cbaf10bcc"/> |
114 | 114 | |
115 | - <activity android:name=".ui.activity.StartActivity" | |
115 | + <activity | |
116 | + android:name=".ui.activity.StartActivity" | |
116 | 117 | android:launchMode="singleInstance" |
117 | 118 | android:windowSoftInputMode="adjustPan|stateAlwaysHidden"> |
118 | 119 | <intent-filter> |
... | ... | @@ -121,10 +122,10 @@ |
121 | 122 | <category android:name="android.intent.category.LAUNCHER"/> |
122 | 123 | </intent-filter> |
123 | 124 | </activity> |
124 | - <!--<activity--> | |
125 | - <!--android:name=".ui.MainActivity"--> | |
126 | - <!--android:screenOrientation="portrait"--> | |
127 | - <!--android:windowSoftInputMode="adjustPan|stateAlwaysHidden" />--> | |
125 | + <!-- <activity --> | |
126 | + <!-- android:name=".ui.MainActivity" --> | |
127 | + <!-- android:screenOrientation="portrait" --> | |
128 | + <!-- android:windowSoftInputMode="adjustPan|stateAlwaysHidden" /> --> | |
128 | 129 | <activity |
129 | 130 | android:name=".ui.activity.MyChildActivity" |
130 | 131 | android:screenOrientation="portrait"/> |
... | ... | @@ -133,12 +134,12 @@ |
133 | 134 | <activity |
134 | 135 | android:name=".ui.MainActivity" |
135 | 136 | android:launchMode="singleInstance" |
136 | - android:windowSoftInputMode="adjustPan|stateAlwaysHidden" | |
137 | - android:screenOrientation="portrait"/> | |
137 | + android:screenOrientation="portrait" | |
138 | + android:windowSoftInputMode="adjustPan|stateAlwaysHidden"/> | |
138 | 139 | <activity |
139 | 140 | android:name=".ui.activity.ChildDetialActivity" |
140 | 141 | android:launchMode="singleInstance" |
141 | - android:screenOrientation="portrait" /> | |
142 | + android:screenOrientation="portrait"/> | |
142 | 143 | <activity |
143 | 144 | android:name=".ui.activity.binding.CreateChildInfoActivity" |
144 | 145 | android:launchMode="singleInstance" |
... | ... | @@ -148,37 +149,38 @@ |
148 | 149 | android:name=".ui.activity.binding.SelectSchoolActivity" |
149 | 150 | android:screenOrientation="portrait" |
150 | 151 | android:windowSoftInputMode="adjustPan|stateHidden"/> |
151 | - | |
152 | - <activity android:name=".ui.activity.LoginAndRegistActivity" | |
153 | - android:screenOrientation="portrait" | |
154 | - /> | |
152 | + <activity | |
153 | + android:name=".ui.activity.LoginAndRegistActivity" | |
154 | + android:screenOrientation="portrait"/> | |
155 | 155 | <activity |
156 | 156 | android:name=".ui.activity.PersonInfoActivity" |
157 | 157 | android:launchMode="singleInstance" |
158 | 158 | android:screenOrientation="portrait" |
159 | 159 | android:windowSoftInputMode="adjustPan|stateHidden"/> |
160 | - | |
161 | 160 | <activity |
162 | 161 | android:name=".ui.activity.binding.CheckInfoActivity" |
163 | 162 | android:launchMode="singleInstance" |
164 | 163 | android:screenOrientation="portrait" |
165 | 164 | android:windowSoftInputMode="adjustPan|stateHidden"/> |
166 | - | |
167 | 165 | <activity |
168 | 166 | android:name=".ui.activity.binding.InviteCodeActivity" |
169 | 167 | android:launchMode="singleInstance" |
170 | - android:screenOrientation="portrait" /> | |
171 | - | |
168 | + android:screenOrientation="portrait"/> | |
172 | 169 | <activity |
173 | 170 | android:name=".ui.activity.mywebview.WebViewActivity" |
174 | 171 | android:launchMode="singleInstance" |
175 | - android:screenOrientation="portrait" /> | |
176 | - | |
177 | - <activity android:name=".ui.activity.consult.ConsultTwoLevelActivity" | |
172 | + android:screenOrientation="portrait"/> | |
173 | + <activity | |
174 | + android:name=".ui.activity.consult.ConsultTwoLevelActivity" | |
178 | 175 | android:launchMode="singleInstance"/> |
179 | - | |
180 | - <activity android:name=".ui.activity.orderdetail.OrderDetailActivity" | |
176 | + <activity | |
177 | + android:name=".ui.activity.orderdetail.OrderDetailActivity" | |
181 | 178 | android:launchMode="singleInstance"/> |
179 | + | |
180 | + <service | |
181 | + android:name=".ui.service.BadgeIntentService" | |
182 | + android:exported="false"> | |
183 | + </service> | |
182 | 184 | </application> |
183 | 185 | |
184 | 186 | </manifest> |
185 | 187 | \ No newline at end of file | ... | ... |
app/src/main/java/com/shunzhi/parent/AppConfig.java
... | ... | @@ -22,6 +22,8 @@ public class AppConfig { |
22 | 22 | public static String USER_SEX = "user_sex"; |
23 | 23 | public static String USER_IMAGE = "user_image"; |
24 | 24 | public static String PARENT_ID = "parent_id"; |
25 | + public static String NIM_CONFIG_VIBRATE = "nim_config_vibrate"; | |
26 | + public static String NIM_CONFIG_SOUND = "nim_config_sound"; | |
25 | 27 | |
26 | 28 | |
27 | 29 | public static boolean ISLOGIN = false; | ... | ... |
app/src/main/java/com/shunzhi/parent/bean/message/PHMessage.java
1 | 1 | package com.shunzhi.parent.bean.message; |
2 | 2 | |
3 | +import com.shunzhi.parent.AppContext; | |
4 | + | |
3 | 5 | import org.greenrobot.greendao.annotation.Entity; |
4 | 6 | import org.greenrobot.greendao.annotation.Id; |
7 | +import org.greenrobot.greendao.annotation.Keep; | |
5 | 8 | import org.greenrobot.greendao.annotation.NotNull; |
6 | 9 | |
10 | +import java.util.Collection; | |
11 | +import java.util.Collections; | |
7 | 12 | import java.util.Date; |
13 | +import java.util.List; | |
8 | 14 | |
9 | 15 | import org.greenrobot.greendao.annotation.Generated; |
10 | 16 | |
... | ... | @@ -26,11 +32,13 @@ public class PHMessage { |
26 | 32 | private String fileUrl; |
27 | 33 | private String messageType; |
28 | 34 | private Integer sendState;//消息发送的状态 1未发送(或者null,不建议使用null) 2发送中 3发送成功 4发送失败 |
35 | + private Integer sessionType; | |
29 | 36 | |
30 | - @Generated(hash = 1293964777) | |
37 | + @Generated(hash = 1935192071) | |
31 | 38 | public PHMessage(Long id, Date date, String messageId, String sessionId, |
32 | 39 | @NotNull String messageText, String exValue, String filePath, |
33 | - String fileUrl, String messageType, Integer sendState) { | |
40 | + String fileUrl, String messageType, Integer sendState, | |
41 | + Integer sessionType) { | |
34 | 42 | this.id = id; |
35 | 43 | this.date = date; |
36 | 44 | this.messageId = messageId; |
... | ... | @@ -41,12 +49,31 @@ public class PHMessage { |
41 | 49 | this.fileUrl = fileUrl; |
42 | 50 | this.messageType = messageType; |
43 | 51 | this.sendState = sendState; |
52 | + this.sessionType = sessionType; | |
44 | 53 | } |
45 | 54 | |
46 | 55 | @Generated(hash = 1001489632) |
47 | 56 | public PHMessage() { |
48 | 57 | } |
49 | 58 | |
59 | + | |
60 | + @Keep | |
61 | + public static long findUnreadNotificationCountBySessionType(Collection<Integer> types) { | |
62 | + PHMessageDao dao = AppContext.getInstance().getDaoSession().getPHMessageDao(); | |
63 | + return dao.queryBuilder().where(PHMessageDao.Properties.SessionType.in(types)).count(); | |
64 | + } | |
65 | + | |
66 | + @Keep | |
67 | + public static long findUnreadNotificationCount() { | |
68 | + PHMessageDao dao = AppContext.getInstance().getDaoSession().getPHMessageDao(); | |
69 | + return dao.queryBuilder() | |
70 | + .where(PHMessageDao.Properties.SessionType.in( | |
71 | + PHMessageSession.sessionType_homework, | |
72 | + PHMessageSession.sessionType_notification//新加通知类型请在此处加上,否则会导致计数不准确 | |
73 | + )) | |
74 | + .count(); | |
75 | + } | |
76 | + | |
50 | 77 | public Long getId() { |
51 | 78 | return this.id; |
52 | 79 | } |
... | ... | @@ -126,4 +153,12 @@ public class PHMessage { |
126 | 153 | public void setSendState(Integer sendState) { |
127 | 154 | this.sendState = sendState; |
128 | 155 | } |
156 | + | |
157 | + public Integer getSessionType() { | |
158 | + return this.sessionType; | |
159 | + } | |
160 | + | |
161 | + public void setSessionType(Integer sessionType) { | |
162 | + this.sessionType = sessionType; | |
163 | + } | |
129 | 164 | } | ... | ... |
app/src/main/java/com/shunzhi/parent/bean/message/PHMessageSession.java
1 | 1 | package com.shunzhi.parent.bean.message; |
2 | 2 | |
3 | +import com.shunzhi.parent.AppConfig; | |
4 | +import com.shunzhi.parent.AppContext; | |
5 | + | |
3 | 6 | import org.greenrobot.greendao.annotation.Entity; |
4 | 7 | import org.greenrobot.greendao.annotation.Id; |
5 | 8 | |
6 | 9 | import java.util.Date; |
7 | 10 | |
8 | 11 | import org.greenrobot.greendao.annotation.Generated; |
12 | +import org.greenrobot.greendao.annotation.Keep; | |
9 | 13 | |
10 | 14 | /** |
11 | 15 | * Created by 10501 on 2018/3/20. |
... | ... | @@ -24,10 +28,15 @@ public class PHMessageSession { |
24 | 28 | private String exValues; |
25 | 29 | private Boolean isTop; |
26 | 30 | |
31 | + @Keep | |
32 | + public static final int sessionType_notification = 101; | |
33 | + @Keep | |
34 | + public static final int sessionType_homework = 102; | |
35 | + | |
27 | 36 | @Generated(hash = 1156938953) |
28 | 37 | public PHMessageSession(Long id, String sessionId, String sessionText, String sessionName, |
29 | - Date date, Integer sessionType, Integer newMessageCount, Integer unReadMessageCount, | |
30 | - String exValues, Boolean isTop) { | |
38 | + Date date, Integer sessionType, Integer newMessageCount, Integer unReadMessageCount, | |
39 | + String exValues, Boolean isTop) { | |
31 | 40 | this.id = id; |
32 | 41 | this.sessionId = sessionId; |
33 | 42 | this.sessionText = sessionText; |
... | ... | @@ -44,6 +53,28 @@ public class PHMessageSession { |
44 | 53 | public PHMessageSession() { |
45 | 54 | } |
46 | 55 | |
56 | + @Keep | |
57 | + public static PHMessageSession findAndCreateSession(int sessionType, boolean autoCreate) { | |
58 | + PHMessageSession session = findSessionByType(sessionType); | |
59 | + if (autoCreate && session == null) { | |
60 | + session = new PHMessageSession(); | |
61 | + switch (sessionType) { | |
62 | + case sessionType_homework: | |
63 | + session.setSessionId(AppConfig.getAppConfig(AppContext.getInstance()).get(AppConfig.USER_ID) + "_homework_" + System.currentTimeMillis()); | |
64 | + session.setSessionName("作业通知"); | |
65 | + break; | |
66 | + } | |
67 | + AppContext.getInstance().getDaoSession().getPHMessageSessionDao().insert(session); | |
68 | + } | |
69 | + return session; | |
70 | + } | |
71 | + | |
72 | + @Keep | |
73 | + public static PHMessageSession findSessionByType(int sessionType) { | |
74 | + PHMessageSessionDao dao = AppContext.getInstance().getDaoSession().getPHMessageSessionDao(); | |
75 | + return dao.queryBuilder().where(PHMessageSessionDao.Properties.SessionType.eq(sessionType)).unique(); | |
76 | + } | |
77 | + | |
47 | 78 | public Long getId() { |
48 | 79 | return this.id; |
49 | 80 | } | ... | ... |
app/src/main/java/com/shunzhi/parent/manager/MessageManager.java
1 | 1 | package com.shunzhi.parent.manager; |
2 | 2 | |
3 | +import android.app.Notification; | |
4 | +import android.app.NotificationManager; | |
5 | +import android.app.PendingIntent; | |
3 | 6 | import android.content.Context; |
4 | 7 | import android.content.Intent; |
5 | 8 | import android.content.SharedPreferences; |
6 | -import android.support.annotation.NonNull; | |
9 | +import android.support.v4.app.NotificationCompat; | |
7 | 10 | |
8 | 11 | import com.netease.nimlib.sdk.NIMClient; |
9 | 12 | import com.netease.nimlib.sdk.Observer; |
... | ... | @@ -16,14 +19,26 @@ import com.netease.nimlib.sdk.msg.MsgServiceObserve; |
16 | 19 | import com.netease.nimlib.sdk.msg.model.CustomNotification; |
17 | 20 | import com.netease.nimlib.sdk.msg.model.IMMessage; |
18 | 21 | import com.share.mvpsdk.utils.ToastUtils; |
22 | +import com.shunzhi.parent.AppConfig; | |
19 | 23 | import com.shunzhi.parent.AppContext; |
24 | +import com.shunzhi.parent.R; | |
20 | 25 | import com.shunzhi.parent.bean.NIMLoginResultBean; |
26 | +import com.shunzhi.parent.bean.message.PHMessage; | |
27 | +import com.shunzhi.parent.bean.message.PHMessageDao; | |
28 | +import com.shunzhi.parent.bean.message.PHMessageSession; | |
29 | +import com.shunzhi.parent.ui.MainActivity; | |
21 | 30 | import com.shunzhi.parent.ui.activity.LoginAndRegistActivity; |
31 | +import com.shunzhi.parent.ui.service.BadgeIntentService; | |
22 | 32 | |
33 | +import org.json.JSONException; | |
34 | +import org.json.JSONObject; | |
35 | + | |
36 | +import java.util.Date; | |
23 | 37 | import java.util.List; |
24 | 38 | |
25 | 39 | import io.reactivex.Observable; |
26 | 40 | import io.reactivex.subjects.PublishSubject; |
41 | +import me.leolin.shortcutbadger.ShortcutBadger; | |
27 | 42 | import timber.log.Timber; |
28 | 43 | |
29 | 44 | /** |
... | ... | @@ -32,6 +47,8 @@ import timber.log.Timber; |
32 | 47 | |
33 | 48 | public class MessageManager { |
34 | 49 | |
50 | + public boolean isNotiNotification = true; | |
51 | + | |
35 | 52 | private static class InstanceHolder { |
36 | 53 | static final MessageManager instance = new MessageManager(); |
37 | 54 | } |
... | ... | @@ -75,10 +92,79 @@ public class MessageManager { |
75 | 92 | |
76 | 93 | } |
77 | 94 | |
78 | - private void onCustomNotificationReceive(CustomNotification customnotification) { | |
95 | + private void onCustomNotificationReceive(CustomNotification customNotification) { | |
96 | + try { | |
97 | + JSONObject json = new JSONObject(customNotification.getContent()); | |
98 | + String type = json.optString("type"); | |
99 | + String uuid = json.optString("msgId"); | |
100 | + if ("homework".equals(type)) { | |
101 | + PHMessageSession session = PHMessageSession.findAndCreateSession(PHMessageSession.sessionType_homework, true); | |
102 | + session.setSessionName("作业通知"); | |
103 | + session.setDate(new Date(customNotification.getTime())); | |
104 | + session.setSessionText(json.optString("title")); | |
105 | + PHMessage message = new PHMessage(); | |
106 | + PHMessageDao messageDao = AppContext.getInstance().getDaoSession().getPHMessageDao(); | |
107 | + message.setDate(new Date(customNotification.getTime())); | |
108 | + message.setMessageId(uuid); | |
109 | + message.setSessionId(session.getSessionId()); | |
110 | + message.setSessionType(session.getSessionType()); | |
111 | + Long id = messageDao.insert(message); | |
112 | + notifyNotification(uuid, id, "收到一条作业通知"); | |
113 | + } | |
114 | + | |
115 | + | |
116 | + } catch (JSONException e) { | |
117 | + e.printStackTrace(); | |
118 | + } | |
119 | + } | |
120 | + | |
121 | + public void notifyNotification(String uuid, long msgLongId, String text) { | |
122 | + if (!isNotiNotification) | |
123 | + return; | |
124 | + int notificationCount = (int) PHMessage.findUnreadNotificationCount();//XSTMessage.findUnreadNotificationCount(); | |
125 | + boolean setBadgerSuccessful = ShortcutBadger.applyCount(AppContext.getInstance(), notificationCount); | |
126 | + Timber.i("----== notificationCount : %s ,setBadgerSuccessful : %s", notificationCount, setBadgerSuccessful); | |
127 | + if (!setBadgerSuccessful) { | |
128 | + BadgeIntentService.start(AppContext.getInstance(), notificationCount, "通知", text, uuid, msgLongId); | |
129 | + return; | |
130 | + } | |
131 | + Intent resultIntent = new Intent(AppContext.getInstance(), MainActivity.class); | |
132 | + resultIntent.putExtra("sid", msgLongId); | |
133 | + resultIntent.putExtra("uuid", uuid); | |
134 | + //resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
135 | + PendingIntent resultPendingIntent = | |
136 | + PendingIntent.getActivity( | |
137 | + AppContext.getInstance(), | |
138 | + 0, | |
139 | + resultIntent, | |
140 | + PendingIntent.FLAG_UPDATE_CURRENT | |
141 | + ); | |
142 | + NotificationCompat.Builder mBuilder = | |
143 | + new NotificationCompat.Builder(AppContext.getInstance()) | |
144 | + .setSmallIcon(R.mipmap.ic_launcher) | |
145 | + .setContentTitle("通知") | |
146 | + .setContentText(text) | |
147 | + //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE) | |
148 | + .setAutoCancel(true) | |
149 | + .setContentIntent(resultPendingIntent); | |
150 | + AppConfig config = AppConfig.getAppConfig(AppContext.getInstance()); | |
151 | + boolean vibrate = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_VIBRATE));//XSTApp.instance.getVibrateState(); | |
152 | + boolean sound = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_SOUND));//XSTApp.instance.getRingState(); | |
153 | + if (vibrate && sound) { | |
154 | + mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); | |
155 | + } else if (vibrate) { | |
156 | + mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); | |
157 | + } else if (sound) { | |
158 | + mBuilder.setDefaults(Notification.DEFAULT_SOUND); | |
159 | + } | |
160 | + NotificationManager notifyMgr = | |
161 | + (NotificationManager) AppContext.getInstance().getSystemService(Context.NOTIFICATION_SERVICE); | |
162 | + notifyMgr.notify((int) msgLongId, mBuilder.build()); | |
163 | + | |
79 | 164 | |
80 | 165 | } |
81 | 166 | |
167 | + | |
82 | 168 | public Observable<NIMLoginResultBean> login(String account, String password) { |
83 | 169 | final PublishSubject<NIMLoginResultBean> subject = PublishSubject.create(); |
84 | 170 | SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE); |
... | ... | @@ -91,7 +177,7 @@ public class MessageManager { |
91 | 177 | public void onSuccess(Object o) { |
92 | 178 | NIMLoginResultBean bean = new NIMLoginResultBean(true, 200, null); |
93 | 179 | subject.onNext(bean); |
94 | - Timber.i("----===onSuccess : %s",o); | |
180 | + Timber.i("----===onSuccess : %s", o); | |
95 | 181 | } |
96 | 182 | |
97 | 183 | @Override | ... | ... |
app/src/main/java/com/shunzhi/parent/ui/MainActivity.java
1 | 1 | package com.shunzhi.parent.ui; |
2 | 2 | |
3 | +import android.content.Intent; | |
3 | 4 | import android.os.Bundle; |
4 | 5 | import android.support.annotation.NonNull; |
5 | 6 | import android.support.design.widget.BottomNavigationView; |
... | ... | @@ -12,6 +13,10 @@ import com.share.mvpsdk.helper.BottomNavigationViewHelper; |
12 | 13 | import com.share.mvpsdk.utils.PermissionUtils; |
13 | 14 | import com.shunzhi.parent.AppContext; |
14 | 15 | import com.shunzhi.parent.R; |
16 | +import com.shunzhi.parent.bean.message.PHMessage; | |
17 | +import com.shunzhi.parent.bean.message.PHMessageDao; | |
18 | +import com.shunzhi.parent.bean.message.PHMessageSession; | |
19 | +import com.shunzhi.parent.manager.MessageManager; | |
15 | 20 | import com.shunzhi.parent.ui.fragment.CePingFragment; |
16 | 21 | import com.shunzhi.parent.ui.fragment.ConsultFragment; |
17 | 22 | import com.shunzhi.parent.ui.fragment.MineFragment; |
... | ... | @@ -102,11 +107,33 @@ public class MainActivity extends BaseCompatActivity implements PermissionUtils. |
102 | 107 | } |
103 | 108 | |
104 | 109 | @Override |
110 | + protected void onNewIntent(Intent intent) { | |
111 | + super.onNewIntent(intent); | |
112 | + handIntent(intent); | |
113 | + } | |
114 | + | |
115 | + private void handIntent(Intent intent) { | |
116 | + long msgLongId = intent.getLongExtra("sid", -1); | |
117 | + PHMessageDao dao = AppContext.getInstance().getDaoSession().getPHMessageDao(); | |
118 | + PHMessage message = dao.queryBuilder().where(PHMessageDao.Properties.Id.eq(msgLongId)).unique(); | |
119 | + if (message == null) return; | |
120 | + if (message.getSessionType() == PHMessageSession.sessionType_homework) { | |
121 | + //TODO 跳转 | |
122 | + } | |
123 | + } | |
124 | + | |
125 | + @Override | |
105 | 126 | protected int getLayoutId() { |
106 | 127 | return R.layout.activity_main; |
107 | 128 | } |
108 | 129 | |
109 | 130 | @Override |
131 | + protected void onPause() { | |
132 | + super.onPause(); | |
133 | + MessageManager.getInstance().isNotiNotification = true; | |
134 | + } | |
135 | + | |
136 | + @Override | |
110 | 137 | protected void onDestroy() { |
111 | 138 | super.onDestroy(); |
112 | 139 | AppContext.getInstance().destoryLocation(); |
... | ... | @@ -120,5 +147,6 @@ public class MainActivity extends BaseCompatActivity implements PermissionUtils. |
120 | 147 | @Override |
121 | 148 | protected void onResume() { |
122 | 149 | super.onResume(); |
150 | + MessageManager.getInstance().isNotiNotification = false; | |
123 | 151 | } |
124 | 152 | } | ... | ... |
app/src/main/java/com/shunzhi/parent/ui/service/BadgeIntentService.java
0 → 100644
... | ... | @@ -0,0 +1,143 @@ |
1 | +package com.shunzhi.parent.ui.service; | |
2 | + | |
3 | +import android.app.IntentService; | |
4 | +import android.app.Notification; | |
5 | +import android.app.NotificationManager; | |
6 | +import android.app.PendingIntent; | |
7 | +import android.content.Context; | |
8 | +import android.content.Intent; | |
9 | +import android.os.Build; | |
10 | +import android.support.annotation.Nullable; | |
11 | +import android.support.v4.app.NotificationCompat; | |
12 | +import android.text.TextUtils; | |
13 | + | |
14 | +import com.shunzhi.parent.AppConfig; | |
15 | +import com.shunzhi.parent.AppContext; | |
16 | +import com.shunzhi.parent.R; | |
17 | +import com.shunzhi.parent.ui.MainActivity; | |
18 | + | |
19 | +import me.leolin.shortcutbadger.ShortcutBadger; | |
20 | + | |
21 | + | |
22 | +/** | |
23 | + * An {@link IntentService} subclass for handling asynchronous task requests in | |
24 | + * a service on a separate handler thread. | |
25 | + * <p> | |
26 | + * TODO: Customize class - update intent actions and extra parameters. | |
27 | + */ | |
28 | +public class BadgeIntentService extends IntentService { | |
29 | + private int notificationId = 0; | |
30 | + | |
31 | + public BadgeIntentService() { | |
32 | + super("BadgeIntentService"); | |
33 | + } | |
34 | + | |
35 | + private NotificationManager mNotificationManager; | |
36 | + | |
37 | + public static void start(Context context, int badgeCount, String title, String message) { | |
38 | + Intent i = new Intent(context, BadgeIntentService.class); | |
39 | + i.putExtra("badgeCount", badgeCount); | |
40 | + i.putExtra("title", title); | |
41 | + i.putExtra("message", message); | |
42 | + context.startService(i); | |
43 | + } | |
44 | + | |
45 | + public static void start(Context context, int badgeCount, String title, String message, String uuid, long xstsid) { | |
46 | + Intent i = new Intent(context, BadgeIntentService.class); | |
47 | + i.putExtra("badgeCount", badgeCount); | |
48 | + i.putExtra("title", title); | |
49 | + i.putExtra("uuid", uuid); | |
50 | + i.putExtra("xstsid", xstsid); | |
51 | + i.putExtra("message", message); | |
52 | + context.startService(i); | |
53 | + } | |
54 | + | |
55 | + public BadgeIntentService(String name) { | |
56 | + super(name); | |
57 | + } | |
58 | + | |
59 | + @Override | |
60 | + public void onStart(Intent intent, int startId) { | |
61 | + super.onStart(intent, startId); | |
62 | + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + protected void onHandleIntent(Intent intent) { | |
67 | + if (intent != null) { | |
68 | + int badgeCount = intent.getIntExtra("badgeCount", 0); | |
69 | + String title = intent.getStringExtra("title"); | |
70 | + String message = intent.getStringExtra("message"); | |
71 | + //String uuid, long xstsid | |
72 | + String uuid = intent.getStringExtra("uuid"); | |
73 | + long xstsid = intent.getLongExtra("xstsid", -1); | |
74 | + if (xstsid == -1 || TextUtils.isEmpty(uuid)) | |
75 | + createIntentNotification(badgeCount, title, message); | |
76 | + if (xstsid != -1 && !TextUtils.isEmpty(uuid)) | |
77 | + createPendingIntentNotification(badgeCount, title, message, uuid, xstsid); | |
78 | + | |
79 | + } | |
80 | + } | |
81 | + | |
82 | + private void createIntentNotification(int badgeCount, String title, String message) { | |
83 | + if (mNotificationManager == null) { | |
84 | + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
85 | + } | |
86 | + if (title == null) title = ""; | |
87 | + if (message == null) message = ""; | |
88 | + if (mNotificationManager != null) | |
89 | + mNotificationManager.cancel(notificationId); | |
90 | + | |
91 | + notificationId++; | |
92 | + | |
93 | + Notification.Builder builder = new Notification.Builder(getApplicationContext()) | |
94 | + .setContentTitle(title) | |
95 | + .setContentText(message) | |
96 | + .setSmallIcon(R.mipmap.ic_launcher); | |
97 | + Notification notification = Build.VERSION.SDK_INT > 15 ? builder.build() : builder.getNotification(); | |
98 | + ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); | |
99 | + if (mNotificationManager != null) | |
100 | + mNotificationManager.notify(notificationId, notification); | |
101 | + } | |
102 | + | |
103 | + private void createPendingIntentNotification(int badgeCount, String title, String message, String uuid, long xstsid) { | |
104 | + if (mNotificationManager == null) { | |
105 | + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
106 | + } | |
107 | + Intent resultIntent = new Intent(AppContext.getInstance(), MainActivity.class); | |
108 | + resultIntent.putExtra("sid", xstsid); | |
109 | + resultIntent.putExtra("uuid", uuid); | |
110 | + mNotificationManager.cancel(notificationId); | |
111 | + notificationId = (int) xstsid; | |
112 | + //resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
113 | + PendingIntent resultPendingIntent = | |
114 | + PendingIntent.getActivity( | |
115 | + AppContext.getInstance(), | |
116 | + 0, | |
117 | + resultIntent, | |
118 | + PendingIntent.FLAG_UPDATE_CURRENT | |
119 | + ); | |
120 | + NotificationCompat.Builder mBuilder = | |
121 | + new NotificationCompat.Builder(AppContext.getInstance()) | |
122 | + .setSmallIcon(R.mipmap.ic_launcher) | |
123 | + .setContentTitle("通知") | |
124 | + .setContentText(message) | |
125 | + //.setDefaults(Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE) | |
126 | + .setAutoCancel(true) | |
127 | + .setContentIntent(resultPendingIntent); | |
128 | + AppConfig config = AppConfig.getAppConfig(AppContext.getInstance()); | |
129 | + boolean vibrate = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_VIBRATE));//XSTApp.instance.getVibrateState(); | |
130 | + boolean sound = Boolean.parseBoolean(config.get(AppConfig.NIM_CONFIG_SOUND));//XSTApp.instance.getRingState(); | |
131 | + if (vibrate && sound) { | |
132 | + mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE); | |
133 | + } else if (vibrate) { | |
134 | + mBuilder.setDefaults(Notification.DEFAULT_VIBRATE); | |
135 | + } else if (sound) { | |
136 | + mBuilder.setDefaults(Notification.DEFAULT_SOUND); | |
137 | + } | |
138 | + Notification notification = mBuilder.build(); | |
139 | + ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount); | |
140 | + mNotificationManager.notify((int) xstsid, notification); | |
141 | + | |
142 | + } | |
143 | +} | ... | ... |