在使用安卓8.0版本以上手机测试广播通知时,会出现通知不显示的bug
测试广播推送消息的代码时,在电脑的模拟器上可以实现,但是连接到安卓手机的时候就失败,查阅网上的资料,可以发现时安卓版本不兼容的问题。
我用的是bmob云数据库,直接用官方文档中的方法,添加抽象类MyPushMessageReceiver实现广播推送通知,后台推送消息的时候显示并未推送到手机
解决办法:解决安卓兼容问题
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
//builder的channelId需和下面channel的保持一致;
builder.setChannelId("channel_id");
NotificationChannel channel = new
NotificationChannel("channel_id","channel_name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setBypassDnd(true);//设置可以绕过请勿打扰模式
channel.canBypassDnd();//可否绕过请勿打扰模式
//锁屏显示通知
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
channel.shouldShowLights();//是否会闪光
channel.enableLights(true);//闪光
//指定闪光时的灯光颜色,为了兼容低版本在上面builder上通过setLights方法设置了
//channel.setLightColor(Color.RED);
channel.canShowBadge();//桌面launcher消息角标
channel.enableVibration(true);//是否允许震动
//震动模式,第一次100ms,第二次100ms,第三次200ms,为了兼容低版本在上面builder上设置了
//channel.setVibrationPattern(new long[]{100,100,200});
channel.getAudioAttributes();//获取系统通知响铃声音的配置
channel.getGroup();//获取通知渠道组
//绑定通知渠道
manager.createNotificationChannel(channel);
}
上述代码使用了NotificationChannel
(通知渠道)的概念,用来允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。
这里贴出我的MyPushMessageReceiver
广播类的代码,主体是重写的bmob开发文档中原有的广播推送代码
public class MyPushMessageReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// if(intent.getAction().equals(PushConstants.ACTION_MESSAGE)){
// Log.d("bmob", "客户端收到推送内容:"+intent.getStringExtra("msg"));
// }
if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
// 收到广播时,发送一个通知
String jsonStr = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
String content = null;
try {
// 处理JSON
JSONObject jsonObject = new JSONObject(jsonStr);
content = jsonObject.getString("alert");
} catch (JSONException e) {
e.printStackTrace();
}
//获取PendingIntent
Intent mainIntent = new Intent(context, SchoolBabyActivity.class);//此处设置点击跳转的界面
PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
//Notification notify = new Notification.Builder(context);//设置点击内容的点击意图
builder.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)//设置点击后自动清除
.setContentTitle("来自益起的通知")
.setTicker("提示消息来啦!")
.setWhen(System.currentTimeMillis())//设置状态栏里的通知时间
.setContentText(content)//设置通知栏中的文本内容
.setContentIntent(mainPendingIntent)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.logo));
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
//builder的channelId需和下面channel的保持一致;
builder.setChannelId("channel_id");
NotificationChannel channel = new
NotificationChannel("channel_id","channel_name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setBypassDnd(true);//设置可以绕过请勿打扰模式
channel.canBypassDnd();//可否绕过请勿打扰模式
//锁屏显示通知
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
channel.shouldShowLights();//是否会闪光
channel.enableLights(true);//闪光
//指定闪光时的灯光颜色,为了兼容低版本在上面builder上通过setLights方法设置了
//channel.setLightColor(Color.RED);
channel.canShowBadge();//桌面launcher消息角标
channel.enableVibration(true);//是否允许震动
//震动模式,第一次100ms,第二次100ms,第三次200ms,为了兼容低版本在上面builder上设置了
//channel.setVibrationPattern(new long[]{100,100,200});
channel.getAudioAttributes();//获取系统通知响铃声音的配置
channel.getGroup();//获取通知渠道组
//绑定通知渠道
manager.createNotificationChannel(channel);
}
Notification notify1=builder.build();
manager.notify(R.string.app_name, notify1);
}
}
}
同时还可以点击通知消息进入对应的程序界面
最后实现效果如下图
最后成功,这就很奶思了,具体的各项文字图片修改代码可自行查阅。