Android7.0以上通知的解决办法

我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述Android7.0以上通知的解决办法相关的知识,希望能为你提供帮助。
【Android7.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);

 


    推荐阅读