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 注册 广播接收者。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: android.app.remoteserviceexception: bad notification for startforeground是一个异常,通常是由于在启动前台服务时,通知的参数不正确导致的。这个异常通常会在Android应用程序中出现,需要检查代码中的通知参数是否正确,以确保启动前台服务时不会出现异常。如果您需要更多的帮助,请提供更多的上下文信息,以便我们更好地理解您的问题。 ### 回答2: Android.app.remoteserviceexception是一种异常,通常在通过启动前台服务时发生。在Android系统中,前台服务比后台服务更重要,因为它们能够向用户显示通知,使用户了解到正在运行的服务,同时确保系统不会在资源紧张时关闭它们。启动前台服务时,应该向系统提供可让用户了解服务正在运行的通知。 当出现“bad notification for startforeground”异常时,通常表示通知无效或不可用。这可能是因为通知没有正确地设置,或者可能是因为通知复制失败,例如,如果通知超过了允许的大小限制。如果通知无效,它将无法在前台服务启动后正确地显示给用户。 要解决此异常,您需要检查通知的正确性。首先,您需要确保通知具有必要的元素,例如标题和内容。其次,您需要确保正确设置通知的id,以确保通知可以正常显示和更新。还可以检查通知是否越过了允许的大小限制,导致通知无效。 此外,您可以使用日志来检查异常的详细信息,并确定导致该问题的根本原因。通过日志,您可以查看哪些操作导致了异常,以及异常的位置。这可以帮助您快速解决问题,并确保您能够提供正常运行的前台服务。 总之,在启动前台服务时,您应该充分了解通知的重要性,并确保它们得到正确的设置。如果遇到bad notification for startforeground异常,您应该检查通知是否有效和正确设置,以确保正常运行您的前台服务。 ### 回答3: 在 Android 应用程序开发中,有时候会遇到 android.app.remoteserviceexception: bad notification for startforeground 错误。这个错误通常是因为通知栏的设置不正确,而导致的崩溃异常。 在 Android 系统中,使用 Foreground Service 进行长时间运行的任务,需要在通知栏中创建一个持久的通知,来告知用户当前正在后台运行的任务。在创建通知时,需要注意以下几点: 1. 通知的 Channel ID 要正确设置,与 Notification Channel 一致。 2. 通知的 Icon 要正确设置,不能为 0。 3. 通知的 Title 和 Content 要正确设置,以便用户能够理解当前后台运行的任务。 如果通知栏的设置不正确,就可能会引起 android.app.remoteserviceexception: bad notification for startforeground 错误。通常这个错误会在 startForeground 方法被调用时出现,导致应用程序崩溃。 为了解决这个问题,我们可以检查通知栏的设置是否正确,尤其是 Channel ID、Icon、Title 和 Content 等参数。同时,也可以查看日志文件,找出造成错误的具体原因,再进行相应的修复。另外,在 Android 8.0 及以上的版本中,还需要注意是否已经创建了 Notification Channel。如果没有创建 Notification Channel,也会导致类似的错误。 总的来说,android.app.remoteserviceexception: bad notification for startforeground 错误是由于通知栏的设置不正确,而导致的崩溃异常。对于开发者来说,需要仔细检查通知栏的设置,确保各个参数都正确无误,避免出现类似的错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值