图片工具类
private PixelMap getPixelMap(int drawableId) {
InputStream drawableInputStream = null;
try {
drawableInputStream = getResourceManager().getResource(drawableId);
ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
sourceOptions.formatHint = "image/png";
ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
decodingOptions.desiredSize = new Size(0, 0);
decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
return pixelMap;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (drawableInputStream != null) {
drawableInputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
1.普通文本通知
//通知的步骤: 1.创建通知文本内容 2.创建文本通知对象 3.创建发送通知对象的请求对象 4.发布通知
// ——————---------------- 文本通知
//获取按钮
Button btn1=(Button) findComponentById(ResourceTable.Id_btn1);
// 赋值按钮的点击事件
btn1.setClickedListener(component -> {
//定义文本通知内容
// 获取普通文本组件
NotificationRequest.NotificationNormalContent content=new NotificationRequest
.NotificationNormalContent()
// 通知标题
.setTitle("我是标题")
// 通知的内容
.setText("这里面是内容")
// 通知的次要内容
.setAdditionalText("次要内容");
// 根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 创建通知请求对象
// 编号id id一定要不一样否则会顶替掉的
NotificationRequest request=new NotificationRequest(1001)
.setContent(notificationContent);
// 发布通知 需要try catch 通知对象安排进来
try {
NotificationHelper.publishNotification( request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
2.可以跳转的普通文本通知
// ------------------------- 可以跳转的普通文本
//获取按钮
Button btn2=(Button) findComponentById(ResourceTable.Id_btn2);
// 赋值按钮的点击事件
btn2.setClickedListener(component -> {
// 定义文本通知的内容
NotificationRequest.NotificationNormalContent content=new NotificationRequest
.NotificationNormalContent()
.setTitle("可以跳转的标题")
.setText("可以跳转的内容")
.setAdditionalText("可以跳转的次要内容");
// 根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 需要使用IntentAgent对象 哪里创建?
IntentAgent intentAgent=CreatIntenAgent(); // 多了这一步
// 创建通知请求对象
NotificationRequest request=new NotificationRequest(1002)
.setContent(notificationContent)
.setIntentAgent(intentAgent); //多了者一行
// 发布通知
try {
NotificationHelper.publishNotification( request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
创建一个跳转的方法类
//创建一个IntenAgent的方法
public IntentAgentCreatIntenAgent(){
// 创建intent 的对象
Operation operation=new Intent.OperationBuilder()
.withDeviceId("")
.withBundleName(getBundleName())
.withAbilityName("com.text1.tongzhi.MainAbility")
.build();
Intent intent=new Intent();
intent.setOperation(operation);
//把intent对象添加到集合里面去
List intentList=new ArrayList<>();
intentList.add(intent);
//创建flag对象
List flagsList=new ArrayList<>();
// 创建点击效果 只能点击一次 点击完消失
flagsList.add(IntentAgentConstant.Flags.ONE_TIME_FLAG);
// 参数1:请求参数 参数2:操作类型 参数3:标识flag列表 参数4:inttent列表 参数5;针对Fa的启动列表
IntentAgentInfo intentAgentInfo=new IntentAgentInfo(200, IntentAgentConstant.OperationType.
START_ABILITY,flagsList,intentList,null);
// 通过 IntentAgentHelper 创建IntentAgent对象
IntentAgent agent= IntentAgentHelper.getIntentAgent(this,intentAgentInfo);
return agent;
}
3.长文本通知
// -------------------------- 长文本的通知
Button btn3=(Button) findComponentById(ResourceTable.Id_btn3);
btn3.setClickedListener(component -> {
//定义文本通知的内容
NotificationRequest.NotificationLongTextContent content=new NotificationRequest
.NotificationLongTextContent()
.setTitle("我是长文本标题")
.setLongText("我是长文本的内容,这里很长很帐的故事,从前有坐山,山上有坐庙,米哦啊里你猜猜有啥!!")
.setExpandedTitle("拓展标题")
.setExpandedTitle("长文本次要内容");
// 根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
//创建通知请求对象
NotificationRequest request=new NotificationRequest(1003)
.setContent(notificationContent);
// 发布通知
try {
NotificationHelper.publishNotification(request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
4.多行文本通知
// ================ 多行文本通知
Button btn4=(Button) findComponentById(ResourceTable.Id_btn4);
btn4.setClickedListener(component -> {
// 定义文本通知的内容
NotificationRequest.NotificationMultiLineContent content=new NotificationRequest
.NotificationMultiLineContent()
.setTitle("多行文本标题")
.setExpandedTitle("多行文本的拓展标题")
.setText("内容是哈哈哈 ")
.addSingleLine("第一行是1111")
.addSingleLine("第一行是22222")
.addSingleLine("第一行33333")
.addSingleLine("第一行54444")
.addSingleLine("第一行6666");
// 根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 创建通知请求对象
NotificationRequest request=new NotificationRequest(1004)
.setContent(notificationContent);
//发布通知
try {
NotificationHelper.publishNotification(request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
5.图片文本通知
// ================================带图片的通知
Button btn5=(Button) findComponentById(ResourceTable.Id_btn5);
btn5.setClickedListener(component -> {
// 定义文本通知内容
NotificationRequest.NotificationPictureContent content=new NotificationRequest
.NotificationPictureContent()
.setTitle("带图的标题")
.setText("带图的内容通知")
.setBriefText("简介的文本")
.setExpandedTitle("拓展标题")
.setBigPicture(getPixelMap(ResourceTable.Media_image02));
//根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 创建通知请求对象
NotificationRequest request =new NotificationRequest(1005)
.setContent(notificationContent);
//发布通知
try {
NotificationHelper.publishNotification(request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
创建个图片的方法类 直接复制粘贴
// 图片转格式的方法
private PixelMapgetPixelMap(int drawableId) {
InputStream drawableInputStream =null;
try {
drawableInputStream = getResourceManager().getResource(drawableId);
ImageSource.SourceOptions sourceOptions =new ImageSource.SourceOptions();
sourceOptions.formatHint ="image/png";
ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
ImageSource.DecodingOptions decodingOptions =new ImageSource.DecodingOptions();
decodingOptions.desiredSize =new Size(0, 0);
decodingOptions.desiredRegion =new Rect(0, 0, 0, 0);
decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
return pixelMap;
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (drawableInputStream !=null) {
drawableInputStream.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
6.聊天通知
//=====================聊天的通知
Button btn6=(Button) findComponentById(ResourceTable.Id_btn6);
btn6.setClickedListener(component -> {
//对话人员创建
MessageUser user1=new MessageUser();
user1.setName("小虎");
user1.setPixelMap(getPixelMap(ResourceTable.Media_profile_1));
MessageUser user2=new MessageUser();
user2.setName("丽丽");
user2.setPixelMap(getPixelMap(ResourceTable.Media_profile_2));
// 定义通知文本内容
NotificationRequest.NotificationConversationalContent content=new NotificationRequest
.NotificationConversationalContent(user1)
// app名称
.setConversationTitle("微信")
.addConversationalMessage(new NotificationRequest.NotificationConversationalContent
.ConversationalMessage("在吗?外面下雨了", Time.getCurrentTime(),user1))
.addConversationalMessage(new NotificationRequest.
NotificationConversationalContent.ConversationalMessage("是啊,有点冷",
Time.getCurrentTime(),user2));
//根据内容创建通知对象
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 创建通知请求对象
NotificationRequest request =new NotificationRequest(1005)
.setContent(notificationContent);
// 发送通知
try {
NotificationHelper.publishNotification(request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
7.媒体的通知
//====================发布媒体的通知
Button btn7=(Button) findComponentById(ResourceTable.Id_btn7);
btn7.setClickedListener(component -> {
// 定义通知文本内容
NotificationRequest.NotificationMediaContent content=new NotificationRequest
.NotificationMediaContent()
.setTitle("媒体通知标题")
.setText("正在播放:爱你就是")
.setAdditionalText("次要内容");
// 定义三个操作按钮
NotificationActionButton button1=new NotificationActionButton.Builder(
getPixelMap(ResourceTable.Media_play),"开始",null
).build();
NotificationActionButton button2=new NotificationActionButton.Builder(
getPixelMap(ResourceTable.Media_like),"喜欢",null
).build();
NotificationActionButton button3=new NotificationActionButton.Builder(
getPixelMap(ResourceTable.Media_star),"收藏",null
).build();
// 根据内容创建通知
NotificationRequest.NotificationContent notificationContent=new NotificationRequest
.NotificationContent(content);
// 创建通知请求对象 可以添加按钮
NotificationRequest request=new NotificationRequest(1007)
.setContent(notificationContent)
.addActionButton(button1)//需要多写内容 按钮添加进去
.addActionButton(button2)
.addActionButton(button3);
// 发布通知
try {
NotificationHelper.publishNotification(request);
}catch (RemoteException e) {
e.printStackTrace();
}
});
网友评论