Android Studio 通知
创始人
2024-08-16 21:03:49
0次

要在Android Studio中发送通知,可以使用NotificationCompat.Builder类来构建通知。以下是一个简单的示例代码:

// 创建通知渠道(仅适用于Android 8.0及更高版本)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Channel Name";
    String description = "Channel Description";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel("channel_id", name, importance);
    channel.setDescription(description);
    // 注册通知渠道
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

// 创建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("Notification Title")
        .setContentText("Notification Content")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

// 点击通知时打开的Activity
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);

// 发送通知
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());

请确保在AndroidManifest.xml中的标签中添加以下权限:



另外,请注意在Android 8.0及更高版本中,需要创建并注册通知渠道,以便正确显示通知。

相关内容

热门资讯

WPK模拟器!AApoker插... WPK模拟器!AApoker插件辅助器ai,wpk刷入池率脚本,竟然真的是有ai辅助器演示1、让任何...
WPK辅助器!微扑克教程透视挂... WPK辅助器!微扑克教程透视挂ai,AApoker设置,其实真的有ai透视挂方式1、AApoker设...
WPK工具!WePoKer技巧... WPK工具!WePoKer技巧辅助ai,AApoker ai插件,原来有ai辅助烘培在进入AApok...
WPK控制牌!菠萝智星德州插件... WPK控制牌!菠萝智星德州插件透视挂ai,AApoker下载,好像有ai透视挂操作1)智星德州插件免...
WPK四人!德扑之星方法透视挂... WPK四人!德扑之星方法透视挂ai,约局吧app有挂吗,切实真的有ai透视挂资料1)约局吧app有挂...
WPK辅助器!wpk攻略辅助器... WPK辅助器!wpk攻略辅助器ai,智星德州插件最新版本更新内容详解,都是有ai辅助器秘籍1、下载好...
WPK俱乐部!aapoker工... WPK俱乐部!aapoker工具透视挂ai,werplan外卦神器,都是真的有ai透视挂举措1、这是...
WPK透视版!AApoker插... WPK透视版!AApoker插件透视ai,AApoker脚本,总是真的有ai透视资料亲,关键说明,A...
WPK脚本!德州扑克教程开挂a... WPK脚本!德州扑克教程开挂ai,德扑之心免费透视,都是存在有ai开挂学习运德扑之心免费透视辅助工具...
WPK辅助!红龙扑克神器辅助a... WPK辅助!红龙扑克神器辅助ai,来玩app破解版,都是是真的有ai辅助法子1、进入游戏-大厅左侧-...