AndroID 7以上通知采用了通道的概念代码也有所不同,下面提供一个工具类,适配不同版本通知的生成;
public class NotificationUtils extends Contextwrapper {
private notificationmanager manager;
public static final String ID = "channel_1";
public static final String name = "channel_name_1";
public Notification notification;
public int IDd = 0;
public NotificationUtils(Context context){
super(context);
}
@RequiresAPI(API = Build.VERSION_CODES.O)
public voID createNotificationChannel(){
NotificationChannel channel = new NotificationChannel(ID, name, notificationmanager.importANCE_HIGH);
getManager().createNotificationChannel(channel);
}
private notificationmanager getManager(){
if (manager == null){
manager = (notificationmanager) getSystemService(NOTIFICATION_SERVICE);
}
return manager;
}
@RequiresAPI(API = Build.VERSION_CODES.O)
public Notification.Builder getChannelNotification(String Title, String content){
if(Title.contains("继电器状态")){
return new Notification.Builder(getApplicationContext(),ID)
.setContentTitle(Title)
.setContentText("设置继电器状态:"+content)
.setSmallicon(androID.R.drawable.stat_notify_more)
.setSound(Uri.fromfile(new file("/system/media/audio/ringtones/Luna.ogg"))) //通知声音设置
.setVibrate(new long[]{0,1000,1000,1000}) //设置震动
.setlights(color.GREEN,1000,1000) //设置闪光灯提示
.setautoCancel(true);
}else {
Intent intent = new Intent(this, AlarmActivity.class);
intent.setPackage(getPackagename());
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
return new Notification.Builder(getApplicationContext(),ID)
.setContentTitle(Title)
.setContentText(content)
.setSound(Uri.fromfile(new file("/system/media/audio/ringtones/Luna.ogg"))) //通知声音设置
.setVibrate(new long[]{0,1000,1000,1000}) //设置震动
.setlights(color.GREEN,1000,1000) //设置闪光灯提示
.setContentIntent(pi)
.setSmallicon(androID.R.drawable.stat_notify_more)
.setautoCancel(true);
}
}
public NotificationCompat.Builder getNotification_25(String Title, String content){
Intent intent = new Intent(this, AlarmActivity.class);
intent.setPackage(getPackagename());
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
//RemoteVIEws remoteVIEws = new RemoteVIEws(getApplicationContext().getPackagename(),R.layout.activity_alarm);
//remoteVIEws.setonClickPendingIntent(R.ID.alarmVIEw,pi);
return new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(Title)
.setContentText(content)
.setSmallicon( R.mipmap.ic_launcher).setLargeIcon( BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pi)
.setSound(Uri.fromfile(new file("/system/media/audio/ringtones/Luna.ogg"))) //通知声音设置
.setVibrate(new long[]{0,1000,1000,1000}) //设置震动
.setlights(color.GREEN,1000,1000) //设置闪光灯提示
.setautoCancel(true);
// return new Notification.Builder(this).setTicker("123").
// setSmallicon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
// .setContentText("123").setContentTitle( "你有最新的报警信息请点击查看" );
}
//发送通知
@RequiresAPI(API = Build.VERSION_CODES.JELLY_BEAN)
public voID sendNotification(String Title, String content){
if (Build.VERSION.SDK_INT>=26){
createNotificationChannel();
this.notification = getChannelNotification
(Title, content).build();
getManager().notify(1,notification);
}else{
this.notification = getNotification_25(Title, content).build();
getManager().notify(1,notification);
}
}
//获取通知
public Notification getNotification(){
return this.notification;
}
}
调用方法如下:
//生成通知
NotificationUtils notificationUtils = new NotificationUtils(context);
notificationUtils.sendNotification("我是一个通知", info);
总结
以上是内存溢出为你收集整理的Android 7.0以上通知的解决办法全部内容,希望文章能够帮你解决Android 7.0以上通知的解决办法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)