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及更高版本中,需要创建并注册通知渠道,以便正确显示通知。

相关内容

热门资讯

透视规律!wepoker透视脚... 透视规律!wepoker透视脚本下载,htx矩阵wepoker辅助(详细软件)1、构建自己的wepo...
透视软件!aapoker透视脚... 透视软件!aapoker透视脚本下载(透视)aapoker插件下载,细节方法(有挂揭秘)一、aapo...
透视总结!wepoker轻量版... 透视总结!wepoker轻量版书签,wepoker有没有辅助(详细俱乐部辅助);1、下载好wepok...
透视最新!aapoker透视方... 透视最新!aapoker透视方法(透视)aapoker辅助器怎么用,普及教程(有挂黑科技);1、进入...
透视好牌!wepoker辅助器... 透视好牌!wepoker辅助器安装包定制,wepoker能不能透视(详细透视脚本)1、完成wepok...
透视代打!aapoker辅助器... 透视代打!aapoker辅助器怎么用(透视)aapoker怎么设置提高好牌几率,新版2025教程(有...
透视工具!wepoker透视底... 透视工具!wepoker透视底牌,wepoker透视苹果系统(详细有人玩)所有人都在同一条线上,像星...
透视安装!wepoker轻量版... 透视安装!wepoker轻量版透视系统,wepoker买钻石有用吗(详细辅助软件教程)1)wepok...
透视脚本!aapoker透视插... 透视脚本!aapoker透视插件(透视)aapoker辅助工具安全吗,攻略教程(有挂插件);1、aa...
透视中牌率!aapoker俱乐... 透视中牌率!aapoker俱乐部靠谱吗(透视)aapoker怎么选牌,透牌教程(有挂揭秘)在进入aa...