<Notification>的功能与方法

本文介绍了一个简单的Android通知功能实现案例,通过代码展示了如何创建并发送通知、如何设置通知的自动取消、图标、标题及内容等属性,并提供了删除通知的方法。
package com.crazyit.ui.notificationdemo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * 通知的功能与简单用法
 */
public class NotificationActivity extends AppCompatActivity {

    public static int NOTIFITION_ID = 0x123;
    NotificationManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        //获取系统服务 NotificationManager
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    // 发送通知的按钮的点击事件
    public void send(View v) {

        //创建一个 Intent
        Intent intent = new Intent(NotificationActivity.this, OtherActivity.class);
        PendingIntent pi = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);

        Notification notification = new Notification.Builder(this)
                .setAutoCancel(true)  // 设置打开通知之后,自动消失
                .setTicker("有新消息")   //设置显示在状态栏的 通知提示消息
                .setSmallIcon(R.mipmap.ic_launcher) //设置通知的图标
                .setContentTitle("一条心通知") //设置通知内容的 标题
                .setContentText("恭喜你.涨工资啦!转正啦!")  //设置通知内容
                        //设置使用系统默认的声音,默认的 LED灯
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
                .setSound(Uri.parse("android.resourse://org.crazy.ui/" + R.raw.msg))
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pi).build();

        //发送通知
        manager.notify(NOTIFITION_ID,notification);

    }

    //为删除的按钮添加点击事件
    public void del(View view){
        manager.cancel(NOTIFITION_ID);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.crazyit.ui.notificationdemo.NotificationActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:onClick="send"
        android:layout_marginStart="36dp"
        android:layout_marginTop="125dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        android:id="@+id/button2"
        android:onClick="del"
        android:layout_centerVertical="true"
        android:layout_alignStart="@+id/button" />
</RelativeLayout>
![NOtification.Bulider提供的常用方法](https://img-blog.youkuaiyun.com/20160604175813682)
怎么简化这两个函数? @Override public void batchUpdatePushNotificationRules(List<PushNotificationRule> pushNotificationRuleList) { List<String> pushTemplateCacheKeyList = new ArrayList<>(); List<String> persistentTemplateCacheKeyList = new ArrayList<>(); List<String> notificationRuleConfigCacheKeyList = new ArrayList<>(); for (PushNotificationRule dto : pushNotificationRuleList) { NotificationRulePO po = new NotificationRulePO(); BeanUtils.copyProperties(dto, po, COPY_WRITINGS); if (dto.getCopywritingList() != null) { List<Document> docs = dto.getCopywritingList().stream() .map(item -> Document.parse(toJson(item))) .collect(Collectors.toList()); po.setCopywritingList(docs); } notificationRuleService.updateNotificationRule(po); String appCategory = po.getAppCategory(); String notificationType = po.getNotificationType(); String configKey = null; if (po.getScenarioList().contains(Scenario.PUSH_NOTIFICATION)) { configKey = buildKeyList(PUSH_NOTIFICATION, notificationType, appCategory, po.getCopywritingList(), pushTemplateCacheKeyList); } if (po.getScenarioList().contains(Scenario.PERSISTENT_NOTIFICATION)) { configKey = buildKeyList(PERSISTENT_NOTIFICATION, notificationType, appCategory, po.getCopywritingList(), persistentTemplateCacheKeyList); } if (configKey != null) { notificationRuleConfigCacheKeyList.add(configKey); } } cacheRefreshSender.send(RefreshEventType.PUSH_TEMPLATE_CACHE_TYPE.getValue(), pushTemplateCacheKeyList); cacheRefreshSender.send(RefreshEventType.NOTIFICATION_RULE_CONFIG_CACHE_TYPE.getValue(), notificationRuleConfigCacheKeyList); cacheRefreshSender.send(RefreshEventType.PERSISTENT_TEMPLATE_CACHE_TYPE.getValue(), persistentTemplateCacheKeyList); }@Override public void batchUpdateEmailNotificationRules(List<EmailNotificationRule> emailNotificationRuleList) { List<String> emailTemplateCacheKeyList = new ArrayList<>(); List<String> notificationRuleConfigCacheKeyList = new ArrayList<>(); for (EmailNotificationRule dto : emailNotificationRuleList) { NotificationRulePO po = new NotificationRulePO(); BeanUtils.copyProperties(dto, po, COPY_WRITINGS); if (dto.getCopywritingList() != null) { List<Document> docs = dto.getCopywritingList().stream() .map(item -> Document.parse(toJson(item))) .collect(Collectors.toList()); po.setCopywritingList(docs); } notificationRuleService.updateNotificationRule(po); String appCategory = po.getAppCategory(); String notificationType = po.getNotificationType(); String configKey = null; if (po.getScenarioList().contains(Scenario.EMAIL_NOTIFICATION)) { configKey = buildKeyList(EMAIL_NOTIFICATION, notificationType, appCategory, po.getCopywritingList(), emailTemplateCacheKeyList); } if (configKey != null) { notificationRuleConfigCacheKeyList.add(configKey); } } cacheRefreshSender.send(RefreshEventType.EMAIL_TEMPLATE_CACHE_TYPE.getValue(), emailTemplateCacheKeyList); cacheRefreshSender.send(RefreshEventType.NOTIFICATION_RULE_CONFIG_CACHE_TYPE.getValue(), notificationRuleConfigCacheKeyList); }
10-20
你帮我检查下我的代码吧! plugin.xml内容如下: ``` <idea-plugin> <id>com.your.company.unique.plugin.id</id> <name>wkx_plugin</name> <version>1.0</version> <vendor email="wangkexin6@yourcompany.com" url="http://www.hikvision.com">HikVision</vendor> <!-- description><![CDATA[ xml和excel互相转换插件.<br> www.wkx666.com</description --> <!-- change-notes><![CDATA[ Add change notes here.<br> most HTML tags may be used ]]> </change-notes--> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> <idea-version since-build="173.0"/> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products --> <depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> <actions> <action id="myAction" class="com.example.HelloWorldAction" text="My Action" description="My description"> <add-to-group group-id="MainMenu.Tools" anchor="last"/> <keyboard-shortcut first-keystroke="control alt A"/> </action> </actions> </extensions> <actions> <!-- Add your actions here --> </actions> </idea-plugin> ``` HelloWorldAction.kt内容如下: ``` import com.intellij.notification.NotificationDisplayType import com.intellij.notification.NotificationGroup import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent class HelloWorldAction : AnAction() { override fun actionPerformed(event: AnActionEvent) { //这里创建了一个消息提示弹窗,在IDE中展示“Hello World” val notificationGroup = NotificationGroup( "myActionId", NotificationDisplayType.BALLOON, true ) val notification = notificationGroup.createNotification( "chentao Demo", "Hello World", "", NotificationType.INFORMATION ).notify(event.project) //从方法的Event对象中获取到当前IDE正在展示的project,在该project中展示弹窗 } } ```
07-25
内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航避障;②研究智能优化算法(如CPO)在路径规划中的实际部署性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为系统鲁棒性。
<think>我们正在讨论一个特定的Maven依赖项:com.cimc.raffles.groupId下的message-spring-boot-starter:0.0.3-SNAPSHOT。由于这是一个公司内部或特定项目的依赖,公开资料可能有限,但我们可以根据Spring Boot Starter的一般使用方式来推断。 一般来说,Spring Boot Starter的使用方法包括: 1. 在项目的pom.xml文件中添加依赖。 2. 根据该starter提供的功能,进行相应的配置(可能在application.properties或application.yml中)。 3. 在代码中通过注解或自动配置的类来使用功能。 但由于这是一个SNAPSHOT版本,可能需要确保你的Maven仓库设置中包含该SNAPSHOT仓库的地址(如果不在默认的Maven中央仓库中)。 具体步骤: 步骤1:添加依赖 在pom.xml的<dependencies>部分添加: ```xml <dependency> <groupId>com.cimc.raffles</groupId> <artifactId>message-spring-boot-starter</artifactId> <version>0.0.3-SNAPSHOT</version> </dependency> ``` 步骤2:添加仓库配置(如果需要) 如果该SNAPSHOT依赖不在Maven中央仓库,你需要在pom.xml或settings.xml中配置仓库。例如,在pom.xml的<repositories>中添加: ```xml <repository> <id>your-snapshot-repo-id</id> <url>http://path.to/your/snapshot/repo</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> ``` 步骤3:配置属性 查看该starter的文档(如果有)了解可配置属性。通常,Spring Boot Starter会通过application.properties或application.yml进行配置。例如: ```properties # 假设该starter用于消息发送,可能会有如下配置 raffles.message.provider=sms raffles.message.api-key=your-api-key ``` 步骤4:使用功能 该starter应该自动配置一些bean,你可以在你的服务中注入使用。例如: ```java @Service public class MyService { private final MessageService messageService; public MyService(MessageService messageService) { this.messageService = messageService; } public void sendMessage() { messageService.send("Hello, World!"); } } ``` 注意:由于我们没有具体文档,以上代码是假设性的。实际使用需要参考该starter的文档或源代码。 如果无法找到文档,建议: - 联系该组件的维护团队获取文档。 - 查看该组件的源代码,了解自动配置类(通常以*AutoConfiguration结尾)和提供的服务类。 另外,由于是SNAPSHOT版本,版本可能会频繁更新,注意及时更新以获取最新修复。 相关问题: 1. 如何配置Maven以使用私有仓库的SNAPSHOT依赖? 2. Spring Boot Starter的工作原理是什么? 3. 如何自定义一个Spring Boot Starter? </think>### com.cimc.raffles.message-spring-boot-starter:0.0.3-SNAPSHOT 使用方法 #### 1. **添加依赖** 在项目的 `pom.xml` 中添加依赖配置: ```xml <dependency> <groupId>com.cimc.raffles</groupId> <artifactId>message-spring-boot-starter</artifactId> <version>0.0.3-SNAPSHOT</version> </dependency> ``` > ⚠️ 注意:SNAPSHOT 版本需确保 Maven 已配置私有仓库访问权限,例如在 `settings.xml` 中添加: > ```xml > <repository> > <id>cimc-snapshots</id> > <url>http://your-repo-domain/repository/maven-snapshots/</url> > </repository> > ``` #### 2. **配置参数** 在 `application.yml` 中配置消息服务参数(具体参数需参考组件文档): ```yaml raffles: message: enabled: true provider: kafka # 可选:sms/email/kafka endpoints: sms: http://sms-api.example.com kafka: bootstrap-servers: kafka-host:9092 topic: user-messages ``` #### 3. **核心注解使用** 通过 `@EnableMessageService` 启用功能: ```java @SpringBootApplication @EnableMessageService // 激活消息组件 public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` #### 4. **消息发送示例** 注入 `MessageService` 并调用发送接口: ```java @Service public class OrderService { @Autowired private MessageService messageService; // 自动注入组件 public void createOrder(Order order) { // 业务逻辑 messageService.send( MessageBuilder.withPayload(order) .setHeader("MSG_TYPE", "ORDER_CREATED") .build() ); } } ``` #### 5. **消息监听示例** 通过 `@MessageListener` 接收消息: ```java @Component public class NotificationHandler { @MessageListener(topics = "user-messages") public void handleEvent(Message<Order> message) { System.out.println("收到消息: " + message.getPayload()); // 处理业务逻辑 } } ``` #### 6. **高级配置** 自定义消息序列化(若需要): ```java @Configuration public class MessageConfig { @Bean public MessageConverter customConverter() { return new Jackson2JsonMessageConverter(); // JSON序列化 } } ``` ### 常见问题排查 1. **依赖拉取失败** - 检查 Maven 仓库配置是否包含 SNAPSHOT 仓库地址 - 确认网络可访问私有仓库(必要时配置代理) 2. **自动装配失效** - 确保 `spring.factories` 中有配置: ```properties org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.cimc.raffles.message.autoconfigure.MessageAutoConfiguration ``` 3. **消息投递异常** - 检查 `bootstrap-servers`(Kafka)或短信API地址配置 - 查看组件日志:`logging.level.com.cimc.raffles.message=DEBUG` > 💡 建议:由于是 SNAPSHOT 版本,定期执行 `mvn -U clean install` 更新最新快照构建[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值