android7.0 通知组,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);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值