nofitication, 兼容8.0

public void showNotification(String sessionId, String title, String fromNick, String content, List<IMMessage> message) {


        //点击广播监听
        Intent intentClick = new Intent(this, NotificationBroadcastReceiver.class);
        intentClick.setAction("notification_clicked");
        intentClick.putExtra(NotificationBroadcastReceiver.TYPE, (Serializable) message);
        intentClick.putExtra("MESSAGE", "消息");
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, 0, intentClick, PendingIntent.FLAG_ONE_SHOT);
        //cancle广播监听
        Intent intentCancel = new Intent(this, NotificationBroadcastReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra(NotificationBroadcastReceiver.TYPE, (Serializable) message);
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, intentCancel, PendingIntent.FLAG_ONE_SHOT);

Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);//系统铃声

//        Toast.makeText(context, getCurrentProcessName() + title, Toast.LENGTH_SHORT).show();
        if (!BaseApplication.getApplication().isBackground()) {
            return;
        }
        String id = content;
        String name = "我是渠道名字";
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
mChannel.setSound(soundUri,Notification.AUDIO_ATTRIBUTES_DEFAULT);
            mChannel.enableVibration(true);
            mChannel.enableLights(true);

//            Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
            Log.i(TAG, mChannel.toString());
            notificationManager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(this)
                    .setChannelId(id)
//                    .setContentTitle("项目名称一二三...-"+title +"一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十")
                    .setContentTitle("项目名称一二三..-" +"纪实名称一二三四五六七八九十一二三四五六七八九十")
                    .setContentText(fromNick + ":" +"发送消息-"+ content+"一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十")
                    .setColor(DemoCache.getContext().getResources().getColor(R.color.theme_blue))
                    //.setSound(Uri.parse("android.resource://com.netease.nim.demo/raw/msg"))
                    .setLights(Color.GREEN, 1000, 1500)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntentClick)
                    .setDeleteIntent(pendingIntentCancel)
                    .setSmallIcon(R.drawable.ic_notification_log).build();
                    notification.flags = Notification.FLAG_AUTO_CANCEL;
        } else {

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//                    .setContentTitle(title + addNotificationNum(sessionId))
//                    .setContentText(fromNick + ":" + content)
                    .setContentTitle("项目名称一二三..-" +"纪实名称一二三四五六七八九十一二三四五六七八九十")
                    .setContentText(fromNick + ":" +"发送消息-"+ content+"一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十")

                    .setSmallIcon(R.drawable.ic_notification_log)
                    .setColor(DemoCache.getContext().getResources().getColor(R.color.theme_blue))
                    //.setSound(Uri.parse("android.resource://com.netease.nim.demo/raw/msg"))
                    .setLights(Color.GREEN, 1000, 1500)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntentClick)
                    .setDeleteIntent(pendingIntentCancel)
                    .setOngoing(true);
//                    .setChannel(id);//无效
            notification = notificationBuilder.build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
        }
        // id 不一样时候不重复, id一样的时候重复刷新同一条通知
        notificationManager.notify(Integer.parseInt(String.valueOf(System.currentTimeMillis()).substring(4)), notification);

    }

坑:

1. 如果重新设置了channel 可能需要重新卸载再安装才能生效,例如声音

2. 需要设置flags, 小米才会允许删除, 否则必须点进去才会消失

发布了30 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36029400/article/details/86063277