// Copyright (c) 2022 NetEase, Inc. All rights reserved. // Use of this source code is governed by a MIT license that can be // found in the LICENSE file. package com.hbl.yuliao; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.ContentResolver; import android.content.Context; import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.text.TextUtils; import com.hbl.yuliao.main.MainActivity; import com.hbl.yuliao.push.PushUserInfoProvider; import com.hbl.yuliao.utils.Constant; import com.hbl.yuliao.utils.DataUtils; import com.netease.nimlib.sdk.NotificationFoldStyle; import com.netease.nimlib.sdk.SDKOptions; import com.netease.nimlib.sdk.ServerAddresses; import com.netease.nimlib.sdk.StatusBarNotificationConfig; import com.netease.nimlib.sdk.StatusBarNotificationFilter; import com.netease.nimlib.sdk.mixpush.MixPushConfig; import com.netease.yunxin.kit.alog.ALog; import com.netease.yunxin.kit.common.utils.ScreenUtils; import java.io.IOException; /** * Nim SDK config info */ public class NimSDKOptionConfig { public static final String NOTIFY_SOUND_KEY = "android.resource://com.hbl.yuliao/raw/xxmoren"; public static final int LED_ON_MS = 1000; public static final int LED_OFF_MS = 1500; static SDKOptions getSDKOptions(Context context, String appKey) { SDKOptions options = new SDKOptions(); options.appKey = appKey; initStatusBarNotificationConfig(options); options.sdkStorageRootPath = getAppCacheDir(context); // options.statusBarNotificationConfig.notificationEntrance = MainActivity.class;// options.preloadAttach = true; options.thumbnailSize = (int) (222.0 / 375.0 * ScreenUtils.getDisplayWidth()); options.userInfoProvider = new PushUserInfoProvider(context); options.sessionReadAck = true; options.animatedImageThumbnailEnabled = true; options.asyncInitSDK = true; options.reducedIM = false; options.checkManifestConfig = false; options.enableTeamMsgAck = true; options.enableFcs = false; options.shouldConsiderRevokedMessageUnreadCount = true; // 会话置顶是否漫游 options.notifyStickTopSession = true; options.mixPushConfig = buildMixPushConfig(); options.serverConfig = configServer(context); // 打开消息撤回未读数-1的开关 options.shouldConsiderRevokedMessageUnreadCount = true; return options; } public static ServerAddresses configServer(Context context) { if (DataUtils.getServerConfigType(context) == Constant.OVERSEA_CONFIG) { ServerAddresses serverAddresses = new ServerAddresses(); serverAddresses.lbs = "https://lbs.netease.im/lbs/conf.jsp"; serverAddresses.nosUploadLbs = "http://wannos.127.net/lbs"; serverAddresses.nosUploadDefaultLink = "https://nosup-hz1.127.net"; serverAddresses.nosDownloadUrlFormat = "{bucket}-nosdn.netease.im/{object}"; serverAddresses.nosUpload = "nosup-hz1.127.net"; serverAddresses.nosSupportHttps = true; ALog.d("ServerAddresses", "ServerConfig:use Singapore node"); return serverAddresses; } return null; } public static void initStatusBarNotificationConfig(SDKOptions options) { // load notification // StatusBarNotificationConfig config = loadStatusBarNotificationConfig(); // // load 用户的 StatusBarNotificationConfig 设置项 // // SDK statusBarNotificationConfig 生效 //// config.notificationFilter = //// imMessage -> //// IMApplication.getForegroundActCount() > 0 //// ? StatusBarNotificationFilter.FilterPolicy.DENY //// : StatusBarNotificationFilter.FilterPolicy.DEFAULT; // options.statusBarNotificationConfig = config; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationManager notificationManager = (NotificationManager) IMApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel morenchanel = new NotificationChannel("xxmoren", IMApplication.getAppContext().getString(R.string.bell_default), NotificationManager.IMPORTANCE_DEFAULT); // morenchanel.setDescription("默认默认"); morenchanel.enableLights(true); morenchanel.enableVibration(true); morenchanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxmoren), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(morenchanel); NotificationChannel daqichanel = new NotificationChannel("xxdaqi", IMApplication.getAppContext().getString(R.string.bell_generous), NotificationManager.IMPORTANCE_DEFAULT); // daqichanel.setDescription("大气大气"); daqichanel.enableLights(true); daqichanel.enableVibration(true); daqichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxdaqi), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(daqichanel); NotificationChannel huopochanel = new NotificationChannel("xxhuopo", IMApplication.getAppContext().getString(R.string.bell_lively), NotificationManager.IMPORTANCE_DEFAULT); // huopochanel.setDescription("活泼活泼"); huopochanel.enableLights(true); huopochanel.enableVibration(true); huopochanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxhuopo), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(huopochanel); NotificationChannel jianduanchanel = new NotificationChannel("xxjianduan", IMApplication.getAppContext().getString(R.string.bell_brief), NotificationManager.IMPORTANCE_DEFAULT); // huopochanel.setDescription("活泼活泼"); jianduanchanel.enableLights(true); jianduanchanel.enableVibration(true); jianduanchanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxjianduan), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(jianduanchanel); NotificationChannel keaichanel = new NotificationChannel("xxkeai", IMApplication.getAppContext().getString(R.string.bell_loveliness), NotificationManager.IMPORTANCE_DEFAULT); // huopochanel.setDescription("活泼活泼"); keaichanel.enableLights(true); keaichanel.enableVibration(true); keaichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxkeai), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(keaichanel); NotificationChannel liushuichanel = new NotificationChannel("xxliushui", IMApplication.getAppContext().getString(R.string.bell_flowingwater), NotificationManager.IMPORTANCE_DEFAULT); // huopochanel.setDescription("活泼活泼"); liushuichanel.enableLights(true); liushuichanel.enableVibration(true); liushuichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxliushui), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(liushuichanel); NotificationChannel qingcuichanel = new NotificationChannel("xxqingcui", IMApplication.getAppContext().getString(R.string.bell_clear), NotificationManager.IMPORTANCE_DEFAULT); qingcuichanel.enableLights(true); qingcuichanel.enableVibration(true); qingcuichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxqingcui), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(qingcuichanel); NotificationChannel qingkuaichanel = new NotificationChannel("xxqingkuai", IMApplication.getAppContext().getString(R.string.bell_brisk), NotificationManager.IMPORTANCE_DEFAULT); qingkuaichanel.enableLights(true); qingkuaichanel.enableVibration(true); qingkuaichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxqingkuai), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(qingkuaichanel); NotificationChannel quweichanel = new NotificationChannel("xxquwei", IMApplication.getAppContext().getString(R.string.bell_interest), NotificationManager.IMPORTANCE_DEFAULT); quweichanel.enableLights(true); quweichanel.enableVibration(true); quweichanel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + IMApplication.getAppContext().getPackageName() + "/raw/" + R.raw.xxquwei), Notification.AUDIO_ATTRIBUTES_DEFAULT); notificationManager.createNotificationChannel(quweichanel); } } // config StatusBarNotificationConfig public static StatusBarNotificationConfig loadStatusBarNotificationConfig() { StatusBarNotificationConfig config = new StatusBarNotificationConfig(); config.notificationEntrance = MainActivity.class; config.notificationSmallIconId = R.mipmap.ic_launche; config.notificationColor = Color.parseColor("#3a9efb"); config.notificationSound = null; config.notificationFoldStyle = NotificationFoldStyle.ALL; config.downTimeEnableNotification = true; config.ledARGB = Color.GREEN; config.ledOnMs = LED_ON_MS; config.ledOffMs = LED_OFF_MS; config.showBadge = true; return config; } /** * config app image/voice/file/log directory * /storage/emulated/0/Android/data/com.hbl.yuliao/cache */ static String getAppCacheDir(Context context) { String storageRootPath = null; try { if (context.getExternalCacheDir() != null) { storageRootPath = context.getExternalCacheDir().getCanonicalPath(); } } catch (IOException e) { e.printStackTrace(); } if (TextUtils.isEmpty(storageRootPath)) { storageRootPath = Environment.getExternalStorageDirectory() + "/" + context.getPackageName(); } return storageRootPath; } private static MixPushConfig buildMixPushConfig() { MixPushConfig config = new MixPushConfig(); // xiaomi // config.xmAppId = "xiao mi push app id"; //apply in xiaomi // config.xmAppKey = "xiao mi push app key";//apply in xiaomi // config.xmCertificateName = "Certificate Name";//config in yunxin platform // huawei // config.hwAppId = "huawei app id";//apply in huawei // config.hwCertificateName = "Certificate Name";//config in yunxin platform // meizu // config.mzAppId = "meizu push app id";//apply in meizu // config.mzAppKey = "meizu push app key";//apply in meizu // config.mzCertificateName = "Certificate Name";//config in yunxin platform // fcm config.fcmCertificateName = "DEMO_FCM_PUSH"; // vivo // config.vivoCertificateName = "Certificate Name";//config in yunxin platform // oppo // config.oppoAppId = "oppo push app id";//apply in oppo // config.oppoAppKey = "oppo push app key";//apply in oppo // config.oppoAppSercet = "oppo push app secret"; //apply in oppo // config.oppoCertificateName = "Certificate Name";//config in yunxin platform return config; } }