码迷,mamicode.com
首页 > 移动开发 > 详细

Android 7.0以上通知的解决办法

时间:2020-05-16 20:24:53      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:new   tor   生成   信息   pac   final   nal   string   sap   

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以上通知的解决办法

标签:new   tor   生成   信息   pac   final   nal   string   sap   

原文地址:https://www.cnblogs.com/YunzhuChen/p/12901738.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!