//创建普通通知
String channelId = "测试渠道"; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { Notification notification = new Notification.Builder(getApplicationContext(),channelId) .setContentTitle("测试通知标题") .setContentText("测试通知文本") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.chips) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chips)) .build(); NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel(channelId,"测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); notificationManager.notify((int)Math.random(), notification); }
//创建自定义布局的通知:
MainActivity:
private static final String CHANNEL_ID = "channelID"; private void customNotification() { // Get the layouts to use in the custom notification RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification); // Apply the layouts to the notification Notification customNotification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.chips) .setCustomContentView(rv) .build(); NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); NotificationChannel channel = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { channel = new NotificationChannel(CHANNEL_ID, "测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); } // adding action to left button(添加点击事件) Intent rightIntent = new Intent(this, NotificationIntentService.class); rightIntent.setAction("left"); rv.setOnClickPendingIntent(R.id.button2, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT)); notificationManager.notify((int) Math.random(), customNotification); }
布局:(注意:不可以使用constraint布局)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <Button android:layout_marginRight="5dp" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="show" /> <Button android:layout_marginLeft="5dp" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="hide" /> </LinearLayout>
AndroidManifest.xml(注册service)
<service android:name=".NotificationIntentService"></service>
获取点击事件:
public class NotificationIntentService extends IntentService { public NotificationIntentService() { super("notificationIntentService"); } @Override protected void onHandleIntent(Intent intent) { switch (intent.getAction()){ case "left": android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper()); leftHandler.post(new Runnable() { @Override public void run() { Toast.makeText(getBaseContext(), "You clicked the left button", Toast.LENGTH_LONG).show(); } }); break; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)