11package com .netease .im ;
22
3+ import android .app .NotificationManager ;
34import android .content .Context ;
45import android .graphics .Color ;
56import android .location .LocationProvider ;
1415import com .netease .im .contact .DefalutUserInfoProvider ;
1516import com .netease .im .contact .DefaultContactProvider ;
1617import com .netease .im .login .LoginService ;
18+ import com .netease .im .session .SessionUtil ;
1719import com .netease .im .session .extension .CustomAttachParser ;
1820import com .netease .im .uikit .LoginSyncDataStatusObserver ;
1921import com .netease .im .uikit .cache .DataCacheManager ;
2527import com .netease .im .uikit .contact .core .ContactProvider ;
2628import com .netease .im .uikit .contact .core .query .PinYin ;
2729import com .netease .nimlib .sdk .NIMClient ;
30+ import com .netease .nimlib .sdk .Observer ;
2831import com .netease .nimlib .sdk .SDKOptions ;
2932import com .netease .nimlib .sdk .StatusBarNotificationConfig ;
3033import com .netease .nimlib .sdk .auth .LoginInfo ;
3134import com .netease .nimlib .sdk .mixpush .NIMPushClient ;
3235import com .netease .nimlib .sdk .msg .MessageNotifierCustomization ;
3336import com .netease .nimlib .sdk .msg .MsgService ;
37+ import com .netease .nimlib .sdk .msg .MsgServiceObserve ;
38+ import com .netease .nimlib .sdk .msg .model .CustomNotification ;
3439import com .netease .nimlib .sdk .msg .model .IMMessage ;
3540import com .netease .nimlib .sdk .uinfo .UserInfoProvider ;
3641
4146public class IMApplication {
4247
4348
44-
45-
4649 public static class MiPushConfig {
4750
4851 public String certificate ;
@@ -78,7 +81,7 @@ public MiPushConfig(String certificate, String appID, String appKey) {
7881 private static ImageLoaderKit imageLoaderKit ;
7982 private static StatusBarNotificationConfig statusBarNotificationConfig ;
8083
81- public static void init (Context context , Class mainActivityClass ,@ DrawableRes int notify_msg_drawable_id , MiPushConfig miPushConfig ) {
84+ public static void init (Context context , Class mainActivityClass , @ DrawableRes int notify_msg_drawable_id , MiPushConfig miPushConfig ) {
8285 IMApplication .context = context .getApplicationContext ();
8386 IMApplication .mainActivityClass = mainActivityClass ;
8487 IMApplication .notify_msg_drawable_id = notify_msg_drawable_id ;
@@ -101,8 +104,17 @@ public static void init(Context context, Class mainActivityClass,@DrawableRes in
101104 initKit ();
102105
103106 }
107+ NIMClient .getService (MsgServiceObserve .class ).observeCustomNotification (notificationObserver , true );
104108 }
105109
110+ private static Observer <CustomNotification > notificationObserver = new Observer <CustomNotification >() {
111+ @ Override
112+ public void onEvent (CustomNotification customNotification ) {
113+ NotificationManager notificationManager = (NotificationManager ) IMApplication .getContext ().getSystemService (Context .NOTIFICATION_SERVICE );
114+ SessionUtil .receiver (notificationManager ,customNotification );
115+ }
116+ };
117+
106118 private static boolean inMainProcess (Context context ) {
107119 String packageName = context .getPackageName ();
108120 String processName = SystemUtil .getProcessName (context );
@@ -130,7 +142,7 @@ private static SDKOptions getOptions(Context context) {
130142 SDKOptions options = new SDKOptions ();
131143
132144 // 如果将新消息通知提醒托管给SDK完成,需要添加以下配置。
133- initStatusBarNotificationConfig (options ,context );
145+ initStatusBarNotificationConfig (options , context );
134146
135147 // 配置保存图片,文件,log等数据的目录
136148 String sdkPath = Environment .getExternalStorageDirectory () + "/" + context .getPackageName () + "/nim" ;
@@ -156,6 +168,7 @@ private static SDKOptions getOptions(Context context) {
156168
157169 return options ;
158170 }
171+
159172 // 这里开发者可以自定义该应用初始的 StatusBarNotificationConfig
160173 private static StatusBarNotificationConfig loadStatusBarNotificationConfig (Context context ) {
161174 StatusBarNotificationConfig config = new StatusBarNotificationConfig ();
@@ -164,7 +177,7 @@ private static StatusBarNotificationConfig loadStatusBarNotificationConfig(Conte
164177 config .notificationSmallIconId = notify_msg_drawable_id ;
165178
166179 // 通知铃声的uri字符串
167- config .notificationSound = "android.resource://" + context .getPackageName ()+ "/raw/msg" ;
180+ config .notificationSound = "android.resource://" + context .getPackageName () + "/raw/msg" ;
168181
169182 // 呼吸灯配置
170183 config .ledARGB = Color .GREEN ;
@@ -175,6 +188,7 @@ private static StatusBarNotificationConfig loadStatusBarNotificationConfig(Conte
175188 setStatusBarNotificationConfig (config );
176189 return config ;
177190 }
191+
178192 private static void initStatusBarNotificationConfig (SDKOptions options , Context context ) {
179193 // load 应用的状态栏配置
180194 StatusBarNotificationConfig config = loadStatusBarNotificationConfig (context );
@@ -194,6 +208,7 @@ private static void initStatusBarNotificationConfig(SDKOptions options, Context
194208 // SDK statusBarNotificationConfig 生效
195209 options .statusBarNotificationConfig = userConfig ;
196210 }
211+
197212 private static MessageNotifierCustomization messageNotifierCustomization = new MessageNotifierCustomization () {
198213 @ Override
199214 public String makeNotifyContent (String nick , IMMessage message ) {
@@ -207,10 +222,8 @@ public String makeTicker(String nick, IMMessage message) {
207222 };
208223
209224
210-
211-
212225 /*********************/
213- public static void initKit (){
226+ public static void initKit () {
214227 NIMClient .getService (MsgService .class ).registerCustomAttachmentParser (new CustomAttachParser ());
215228 initUserInfoProvider (userInfoProvider );
216229 initContactProvider (contactProvider );
@@ -236,6 +249,7 @@ public static void initKit(){
236249
237250
238251 }
252+
239253 // 初始化用户信息提供者
240254 private static void initUserInfoProvider (UserInfoProvider userInfoProvider ) {
241255
@@ -255,13 +269,15 @@ private static void initContactProvider(ContactProvider contactProvider) {
255269
256270 IMApplication .contactProvider = contactProvider ;
257271 }
272+
258273 public static UserInfoProvider getUserInfoProvider () {
259274 return userInfoProvider ;
260275 }
261276
262277 public static ContactProvider getContactProvider () {
263278 return contactProvider ;
264279 }
280+
265281 public static ImageLoaderKit getImageLoaderKit () {
266282 return imageLoaderKit ;
267283 }
0 commit comments