使用Notification布局问题导致不显示通知也不报错,代码完整,(关键词:notificationManager.notify;Notification;线性布局;AndroidStudio)

rt,我在照网上搬移了代码后,点击运行,点击测试按钮。没有显示通知栏通知。
解决办法:
1.使用第三方库;
2.将notification所用布局改为线性布局(相对布局没试过,推测可用)。
我之前用的是默认布局ConstraintLayout。

问题:
使用了几个第三方库的带button的消息通知,都不能显示按钮。
测试手机是红米note4x,在模拟器上能正常显示。

ps:
notification学习文章地址:https://blog.csdn.net/qq_35070105/article/details/71983882

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Android Studio 创建通知的示例代码: 1. 在 `AndroidManifest.xml` 文件中添加通知权限: ```xml <uses-permission android:name="android.permission.VIBRATE"/> ``` 2. 在布局文件中创建一个按钮并设置其 `onClick` 事件: ```xml <Button android:id="@+id/notification_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:onClick="sendNotification"/> ``` 3. 在 Activity 中实现 `sendNotification` 方法,该方法创建并发送通知: ```java public void sendNotification(View view) { // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, builder.build()); } ``` 上述代码中,我们创建了一个包含小图标、标题和内容的通知,并将其发送到系统通知栏中。注意,我们需要指定一个通知渠道,这可以在应用启动时进行设置。 4. 在应用启动时创建通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知渠道 CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("my_channel", name, importance); channel.setDescription(description); // 注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 上述代码中,我们检查当前 Android 版本是否支持通知渠道,如果支持,则创建一个默认级别的通知渠道,并在系统中注册该渠道。 现在,当用户点击按钮时,应用将创建并发送一个通知到系统通知栏中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值