Android 7.0以上通知的解决办法

Posted yunzhuchen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 7.0以上通知的解决办法相关的知识,希望对你有一定的参考价值。

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以上通知的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

安卓7.0及以上版本抓包https失败解决方法

Android 7.0 以上 Charles 和 Fiddler 无法抓取 HTTPS 包的解决方式

Android 7.0以上抓包失效

Android 7.0 以上 Charles 和 Fiddler 无法抓取 HTTPS 包的解决方式

Android N 7.0 出现 FileUriExposedException 异常的解决办法

关于在centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法