Discuz!发送系统通知方法notification_add解析

方法定义位置:

source\function\function_core.php

代码如下:

function notification_add($touid, $type, $note, $notevars = array(), $system = 0) {
	return helper_notification::notification_add($touid, $type, $note, $notevars, $system);
}

参数说明:

$touid:接收通知的uid,即发给谁;

$type:通知类型,如system为系统通知,这里$type参数稍微复制一点,下面我们把type参数的各种意义罗列出来供大家参考:

appId(数字)         漫游应用 
myapp                应用邀请或请求 
credit                积分充值 
goods                商品 
mod_member        用户审核 
system                系统消息 
group                群组审核通过 
report                举报 
verify                认证 
manage_                管理通知 
magic                道具 
poke                打招呼 
friend                好友 
task                任务 
wall                留言 
piccomment        图片评论 
blogcomment        日志评论 
sharecomment        分享评论 
follow                关注 
pusearticle        推送 
at                        @功能 
pcomment        点评 
post                回帖引用 
show                排行榜 
clickblog        日志顶操作 
clickarticle 文章顶操作 
clickpic        图片顶操作 
doing                记录 
pmreport        消息举报 
sharenotice        分享通知 
group                群组 
reward                悬赏 
activity        活动 
thread                主题 
blog                日志 
article                文章 

$note:通知内容,支持html代码;

$notevars:附加参数,如:actor、from_num、from_id、from_idtype

$system:是否系统通知,默认0,强制转为系统通知;

详细代码逻辑参考helper_notification类代码

helper类所在位置:

\source\class\helper\helper_notification.php

相关代码如下:

	public static function notification_add($touid, $type, $note, $notevars = array(), $system = 0, $category = -1) {
		global $_G;

		if(!($tospace = getuserbyuid($touid))) {
			return false;
		}
		space_merge($tospace, 'field_home');
		$filter = empty($tospace['privacy']['filter_note'])?array():array_keys($tospace['privacy']['filter_note']);

		if($filter && (in_array($type.'|0', $filter) || in_array($type.'|'.$_G['uid'], $filter))) {
			return false;
		}
		if($category == -1) {
			$category = 0;
			$categoryname = '';
			if($type == 'follow' || $type == 'follower') {
				switch ($type) {
							case 'follow' : $category = 5; break;
							case 'follower' : $category = 6; break;
						}
				$categoryname = $type;
			} else {
				foreach($_G['notice_structure'] as $key => $val) {
					if(in_array($type, $val)) {
						switch ($key) {
							case 'mypost' : $category = 1; break;
							case 'interactive' : $category = 2; break;
							case 'system' : $category = 3; break;
							case 'manage' : $category = 4; break;
							default :  $category = 0;
						}
						$categoryname = $key;
						break;
					}
				}
			}
		} else {
			switch ($category) {
				case 1 : $categoryname = 'mypost'; break;
				case 2 : $categoryname = 'interactive'; break;
				case 3 : $categoryname = 'system'; break;
				case 4 : $categoryname = 'manage'; break;
				case 5 : $categoryname = 'follow'; break;
				case 6 : $categoryname = 'follower'; break;
				default :  $categoryname = 'app';
			}
		}
		if($category == 0) {
			$categoryname = 'app';
		} elseif($category == 1 || $category == 2) {
			$categoryname = $type;
		}
		$notevars['actor'] = "<a href=\"home.php?mod=space&uid=$_G[uid]\">".$_G['member']['username']."</a>";
		if(!is_numeric($type)) {
			$vars = explode(':', $note);
			if(count($vars) == 2) {
				$notestring = lang('plugin/'.$vars[0], $vars[1], $notevars);
			} else {
				$notestring = lang('notification', $note, $notevars);
			}
			$frommyapp = false;
		} else {
			$frommyapp = true;
			$notestring = $note;
		}

		$oldnote = array();
		if($notevars['from_id'] && $notevars['from_idtype']) {
			$oldnote = C::t('home_notification')->fetch_by_fromid_uid($notevars['from_id'], $notevars['from_idtype'], $touid);
		}
		if(empty($oldnote['from_num'])) $oldnote['from_num'] = 0;
		$notevars['from_num'] = $notevars['from_num'] ? $notevars['from_num'] : 1;
		$setarr = array(
			'uid' => $touid,
			'type' => $type,
			'new' => 1,
			'authorid' => $_G['uid'],
			'author' => $_G['username'],
			'note' => $notestring,
			'dateline' => $_G['timestamp'],
			'from_id' => $notevars['from_id'],
			'from_idtype' => $notevars['from_idtype'],
			'from_num' => ($oldnote['from_num']+$notevars['from_num']),
			'category' => $category
		);
		if($system) {
			$setarr['authorid'] = 0;
			$setarr['author'] = '';
		}
		$pkId = 0;
		if($oldnote['id']) {
			C::t('home_notification')->update($oldnote['id'], $setarr);
			$pkId = $oldnote['id'];
		} else {
			$oldnote['new'] = 0;
			$pkId = C::t('home_notification')->insert($setarr, true);
		}
		$banType = array('task');
		if($_G['setting']['cloud_status'] && !in_array($type, $banType)) {
			$noticeService = Cloud::loadClass('Service_Client_Notification');
			if($oldnote['id']) {
				$noticeService->update($touid, $pkId, $setarr['from_num'], $setarr['dateline'], $note);
			} else {
				$extra = $type == 'post' ? array('pId' => $notevars['pid']) : array();
				$extra['notekey'] = $note;
				$noticeService->add($touid, $pkId, $type, $setarr['authorid'], $setarr['author'], $setarr['from_id'], $setarr['from_idtype'], $setarr['note'], $setarr['from_num'], $setarr['dateline'], $extra);
			}
		}

		if(empty($oldnote['new'])) {
			C::t('common_member')->increase($touid, array('newprompt' => 1));
			$newprompt = C::t('common_member_newprompt')->fetch($touid);
			if($newprompt) {
				$newprompt['data'] = unserialize($newprompt['data']);
				if(!empty($newprompt['data'][$categoryname])) {
					$newprompt['data'][$categoryname] = intval($newprompt['data'][$categoryname]) + 1;
				} else {
					$newprompt['data'][$categoryname] = 1;
				}
				C::t('common_member_newprompt')->update($touid, array('data' => serialize($newprompt['data'])));
			} else {
				C::t('common_member_newprompt')->insert($touid, array($categoryname => 1));
			}
			require_once libfile('function/mail');
			$mail_subject = lang('notification', 'mail_to_user');
			sendmail_touser($touid, $mail_subject, $notestring, $frommyapp ? 'myapp' : $type);
		}

		if(!$system && $_G['uid'] && $touid != $_G['uid']) {
			C::t('home_friend')->update_num_by_uid_fuid(1, $_G['uid'], $touid);
		}
	}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
我可以看到这段代码是 Java 语言编写的 Android 应用程序的通知栏更新方法。根据您的要求,我将对其进行修改,但是需要您提供更多上下文和明确的需求。 以下是一种可能的修改方式,该方式将此方法作为类的一部分,并将方法参数更改为更清晰的名称: ```java public class AkiNotificationManager { private static final String TAG = "AkiNotificationManager"; private static final int NOTIFICATION_ID = 1; private NotificationManager mAkiNotificationManager; private Notification.Builder mAkiBuilder; private RemoteViews mAkiRemoteViews; private AkiModel mAkiModel; private String mAkiCurTitle; private String mAkiCurArtist; private String mAkiCurAlbumPath; public AkiNotificationManager(Context context, AkiModel akiModel) { mAkiModel = akiModel; mAkiNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); mAkiBuilder = new Notification.Builder(context); mAkiRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification_layout); mAkiBuilder.setCustomContentView(mAkiRemoteViews); mAkiBuilder.setSmallIcon(R.drawable.ic_music_note); mAkiBuilder.setAutoCancel(false); } public void updateNotification(boolean isPlaying, boolean updateAlbum) { if (isPlaying) { mAkiRemoteViews.setImageViewBitmap(R.id.notification_iv_play, BitmapFactory.decodeResource(getResources(), R.drawable.iv_pause_grey)); } else { mAkiRemoteViews.setImageViewBitmap(R.id.notification_iv_play, BitmapFactory.decodeResource(getResources(), R.drawable.iv_play_grey)); } if (updateAlbum) { mAkiRemoteViews.setTextViewText(R.id.notification_top_song, mAkiCurTitle); mAkiRemoteViews.setTextViewText(R.id.notification_top_singer, mAkiCurArtist); mAkiModel.getAlbumBitmap(bitmapCallBack, mAkiCurAlbumPath); Log.d(TAG, "updateNotification: Updating Album Art: " + mAkiCurAlbumPath); } mAkiBuilder.setCustomContentView(mAkiRemoteViews); mAkiNotificationManager.notify(NOTIFICATION_ID, mAkiBuilder.build()); } private final BitmapCallBack bitmapCallBack = new BitmapCallBack() { @Override public void setBitmap(Bitmap bitmap) { mAkiRemoteViews.setImageViewBitmap(R.id.notification_iv_album, bitmap); mAkiBuilder.setCustomContentView(mAkiRemoteViews); mAkiNotificationManager.notify(NOTIFICATION_ID, mAkiBuilder.build()); } }; public void setCurSong(String title, String artist, String albumPath) { mAkiCurTitle = title; mAkiCurArtist = artist; mAkiCurAlbumPath = albumPath; } } ``` 以上代码包含了一个名为 `AkiNotificationManager` 的类,该类包含了一个在构造函数中初始化的 `NotificationManager`,并且该类包含了一个 `updateNotification` 方法,该方法接受两个参数:一个布尔值,用于指示当前是否正在播放,以及另一个布尔值,用于指示是否需要更新专辑封面。此外,该类还包含一个 `setCurSong` 方法,用于设置当前播放的歌曲信息。 请注意,此代码仅用于演示目的,并且可能需要进一步修改,以符合您的具体需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值