【菜鸟学Android---1】 Notification不显示问题

Notification 不显示问题的解决

今天在写Notification的时候遇到一个问题,点击测试的按钮,无通知显示,并且Toast提示通知无效
查看代码发现所用的方法失效,如下图NotificationCompat.Builder失效
之后便在网上查找解决方案,了解到Android8.0以上需要设置通道信息才可以显示通知,该方法被以下方法替代:

mNotification = new NotificationCompat.Builder(this, "default")

这里新增的 “default” 是一个ChannelId的参数,这个参数无特殊要求,与通知中设置的相同即可(唯一)
知道原因后,对代码进行修改,运行发现还是无法显示,提示default无效,再次检查代码发现,设置的全局变量 mNotification 与 实例化的对象不一致, 即:

private NotificationCompat.Builder mNotification;
/**省略不必要的代码**/
mNotification = new Notification.Builder(context, channelId)
		.setCustomContentView(mRemoteViews);

NotificationCompat 与 Notification不对应,就会报错!
再次修改后运行程序,程序退出报错,查看log:no valid small icon(无有效的小图标)
为mNotification设置小图标:

private Notification.Builder mNotification;
public void createNotify(Context context, int icon) {
        String channelId = "default";
        mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification);
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotification = new Notification.Builder(context, channelId)
                .setSmallIcon(icon)
                .setCustomContentView(mRemoteViews);

修改后Notification可以正常运行了!

第二天

在对Notification进行其他设置后,又无法点击按钮显示通知了,
提示信息:Failed to post notification on channel “default”.
查阅资料发现,需要建立channel
代码如下:

public void createNotify(Context context, String contentText) {
		//channel的id
        String channelId = "channel_1";
        //channel的描述或命名
        String channelName = "EmergencyCall";
        //channel的重要性
        int channelImport = mNotificationManager.IMPORTANCE_DEFAULT;
        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.emergency_call_notification);
		//设置channel
        NotificationChannel channel = new NotificationChannel(channelId, channelName, channelImport);
		//通过manger为通知添加channel
        mNotificationManager.createNotificationChannel(channel);
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setComponent(new ComponentName(context, MainActivity.class));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
        mNotification = new Notification.Builder(context, channelId)
        //这里的channelId与上面设置的一致即可
                .setSmallIcon(R.drawable.sos_notice)
                .setAutoCancel(true)
                .setCustomContentView(mRemoteViews);
        mNotification.setContentIntent(contentIntent);

问题
为什么之前仅仅设置channelId便可以显示通知,之后怎么改都不可以了,必须添加channel才行?
有知道的小伙伴可以在评论留言说一下,谢谢!

总结一下Notification不显示的原因:
1.Android版本更新后,方法失效的处理方法,sdk 26以上要设置添加channel;
2.变量与对象的匹配问题;
3.notification和builder没有设置有效的icon。

最后再记录一个之后可能用到的解决方法,考虑到兼容性的问题需要对版本进行判断(转载别人的):
https://blog.csdn.net/Mr_Leixiansheng/article/details/84942589
https://blog.csdn.net/qq_35749683/article/details/80451791
后面会继续学习了解关于通知渠道的内容。

Android小白,第一次写博客,还在学习阶段,希望大牛指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值