PushChannelManager.uts 4.0 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
import { ChannelManager, SetPushChannelOptions } from "../../interface.uts";
import Context from 'android.content.Context';
import Build from 'android.os.Build';
import NotificationManager from 'android.app.NotificationManager';
import NotificationChannelGroup from 'android.app.NotificationChannelGroup';
import NotificationChannel from 'android.app.NotificationChannel';
import TextUtils from 'android.text.TextUtils';
import ContentResolver from 'android.content.ContentResolver';
import Uri from 'android.net.Uri';
import RingtoneManager from 'android.media.RingtoneManager';

export class PushChannelManager implements ChannelManager {

	static LOCAL_PUSH_CHANNEL_ID = "DcloudChannelID";
	static LOCAL_PUSH_GROUP_ID = "DcloudGroupID";

	private static INSTANCE : PushChannelManager | null = null

	static getInstance() : PushChannelManager {
		if (this.INSTANCE == null) {
			this.INSTANCE = new PushChannelManager()
		}
		return this.INSTANCE!!
	}

	createDefaultChannel(context : Context) {
		if (Build.VERSION.SDK_INT >= 26) {
			const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
			const pChannelId = PushChannelManager.LOCAL_PUSH_CHANNEL_ID
			const pChannelName = context.getResources().getString(R.string.dcloud_feature_aps_notification_channel)
			if (notificationManager.getNotificationChannel(pChannelId) == null) {
				notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(PushChannelManager.LOCAL_PUSH_GROUP_ID, context.getResources().getString(R.string.dcloud_feature_aps_notification_group)))
				const channel = new NotificationChannel(pChannelId, pChannelName, NotificationManager.IMPORTANCE_DEFAULT)
				channel.enableLights(true)
				channel.setShowBadge(true)
				notificationManager.createNotificationChannel(channel)
			}
		}
	}

	/**
	 * 设置推送渠道
	 */
	setPushChannel(options : SetPushChannelOptions) : void {
		if (Build.VERSION.SDK_INT >= 26) {
			const context = UTSAndroid.getAppContext() as Context
			const notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
			if (notificationManager.getNotificationChannel(options.channelId) == null) {
				const notificationChannel = new NotificationChannel(options.channelId, options.channelDesc, NotificationManager.IMPORTANCE_DEFAULT);
				notificationChannel.setShowBadge(true);
				let sound = 0;
				if (!TextUtils.isEmpty(options.soundName)) {
					const packName = context.getApplicationInfo().packageName
					sound = context.getResources().getIdentifier(options.soundName!!, "raw", packName)
				}
				let uriStr = "";
				if (sound != 0) {
					uriStr = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound;
				}

				if (!TextUtils.isEmpty(uriStr)) {
					notificationChannel.setSound(Uri.parse(uriStr), null);
				} else {
					const uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//默认铃音
					notificationChannel.setSound(uri, null);
				}

				if (options.importance != null) {
					notificationChannel.setImportance(options.importance!!.toInt());
				}
				if (options.lockscreenVisibility != null) {
					notificationChannel.setLockscreenVisibility(options.lockscreenVisibility!!.toInt());
				}
				notificationChannel.enableLights(options.enableLights ?? false);
				notificationChannel.enableVibration(options.enableVibration ?? false);
				notificationManager.createNotificationChannel(notificationChannel);
			}
		}
	}
	/**
	 * 获取当前应用注册的所有的通知渠道。
	 */
	getAllChannels() : string[] {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			const context = UTSAndroid.getAppContext() as Context
			const nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
			const channels : string[] = []
			const list = nm.getNotificationChannels()
			for (let i:Int = 0; i < list.size; i++) {
				channels.push(list.get(i).toString())
			}
			return channels
		}else{
			return [] as string[]
		}
	}

}