android notification设置不同字体颜色,notification自定义(动态修改图标,字体颜色)...

【问题】推送样式如下:

99ad6787c706?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

推送新样式.png

【答案】可以。

【知识点】

(1)判断是否开启用户通知权限:

// 判断是否开启 允许通知

NotificationManagerCompat manager = NotificationManagerCompat.from(StarbabaApplication.getInstance().getContext());

// true: 开启

boolean isOpened = manager.areNotificationsEnabled();

(2)判断锁屏通知权限是否开启:

KeyguardManager km = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);

// true: 开启

boolean isScreenOff = km.inKeyguardRestrictedInputMode(); // 是否锁屏

(3)自定义布局RemoteViews:

views = new RemoteViews(mContext.getPackageName(), R.layout.view_notification_routine);

// 设置对应的属性值 (文本)

views.setTextViewText(R.id.tv_appname_routine, BuildConfig.APP_NAME);

// 设置图片(法1)

views.setImageViewBitmap(R.id.iv_pic_routine,bitmap);

// 设置图片(法2)

views.setImageViewBitmap(R.id.iv_pic,BitmapFactory.decodeResource(mContext.getResour ces(), R.drawable.launch_logo));

views.setTextViewText(R.id.tv_title_routine, messageInfo.getTitle());

views.setViewPadding(R.id.tv_title_routine, 0, 5, 0, 0); // 设置间距

views.setTextViewText(R.id.tv_content_routine, messageInfo.getContent());

//views.setViewPadding(R.id.tv_content_routine, 0, -5, 0, 0); // 设置间距

views.setTextViewTextSize(R.id.tv_title, 1, 14); // 设置字体大小

views.setTextViewTextSize(R.id.tv_content, 1, 12);

(4)动态修改 推送logo

【依赖包】 compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

// 一定要先将图片下载下来,再设置上去。

ImageLoader

.getInstance()

.loadImage(messageInfo.getIconUrl(), new ImageLoadingListener() {

@Override

public void onLoadingStarted(String imageUri, View view) {

}

@Override

public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

}

@Override

public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

// imageUri 为图片链接,loadedImage

loadLogo(loadedImage, messageInfo, isNormalStyle);

}

@Override

public void onLoadingCancelled(String imageUri, View view) {

}

});

(5)自定义通知:

resources = mContext.getResources();

notificationManager = (NotificationManager) mContext

.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent();

intent.setAction(IPushConsts.Action.ACTION_HANDLE_MESSAGE);

intent.setClass(mContext, MainService.class);

intent.addCategory(IPushConsts.Category.CATEGORY_PUSH);

intent.setData(Uri.parse("push://"

+ String.valueOf(System.currentTimeMillis())

+ messageInfo.getId()));

intent.putExtra(IPushConsts.Key.KEY_HANDLE_MESSAGE_INFO, messageInfo);

pendingIntent = PendingIntent.getService(mContext, 0,

intent, PendingIntent.FLAG_UPDATE_CURRENT);

// 【注】NotificationCompat.Builder 向下兼容老版本 ,Notification 就没有。

Notification notification = new NotificationCompat.Builder(mContext)

// 不可以不设置这个属性setSmallIcon

.setSmallIcon(R.drawable.vest_yunhuahua_logo) // 消息logo

.setAutoCancel(true)

.setTicker(resources

.getString(“你收到一条新消息了”) // 收到消息的提提示

.setContentTitle(messageInfo.getTitle()) //标题

.setContentText(messageInfo.getContent()) // 内容

// 自定义布局(优点: 可以改变字体样式)

// .setCustomContentView(views)

.setContentIntent(pendingIntent) // 设置 消息点击跳转处理

.setDefaults(Notification.DEFAULT_ALL) // 包括 震动,闪光灯

.getNotification();

notificationManager

.notify(TAG, (int) messageInfo.getId(), notification);

(6)动态改变 自定义布局字体样式,例如加粗, 颜色:

// 目前只发现 这种原始Html样式可以解析 "恭喜你获得2000信用额度"

views.setTextViewText(R.id.tv_title, Html.fromHtml(messageInfo.getTitle()));

【重点】能否监听推送消息被移除?

【答案】可以!!!

【实现】

(1)创建移除Intent:

Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);

intentCancel.setAction("notification_cancelled");

intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, type);

PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);

(2) 使用 setDeleteIntent(pendingIntentCancel)方法:

Notification notification = new NotificationCompat.Builder(mContext)

// 不可以不设置这个属性setSmallIcon

.setSmallIcon(R.drawable.vest_yunhuahua_logo) // 消息logo

.setAutoCancel(true)

.setTicker(resources

.getString(“你收到一条新消息了”) // 收到消息的提提示

.setContentTitle(messageInfo.getTitle()) //标题

.setContentText(messageInfo.getContent()) // 内容

// 自定义布局(优点: 可以改变字体样式)

// .setCustomContentView(views)

.setDeleteIntent(pendingIntentCancel); // 设置消息移除

.setDefaults(Notification.DEFAULT_ALL) // 包括 震动,闪光灯

.getNotification();

(3)broadcastReceiver 消息移除接收器:

public class NotificationBroadcastReceiver extends BroadcastReceiver {

public static final String TYPE = "type"; //这个type是为了Notification更新信息的,这个不明白的朋友可以去搜搜,很多

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

int type = intent.getIntExtra(TYPE, -1);

if (type != -1) {

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// 移除对应某一个消息

notificationManager.cancel(type);

}

if (action.equals("notification_clicked")) {

//处理点击事件

}

if (action.equals("notification_cancelled")) {

//处理滑动清除和点击删除事件

}

}

}

(4) 注意 需要在 Manifest 注册 广播接收者。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值