Android notification 适配不同版本

public class NotificationUtil {

    public static List idNotification = new ArrayList();
    public static int iid;

    public static void showNotification(String uid, String title, String text) {
        iid++;
        NotificationManager notificationManager = (NotificationManager)
                CommonAppContext.sInstance.getSystemService(NOTIFICATION_SERVICE);
        Notification notification = null;
        Intent intent = new Intent(RouteUtil.MAIN);
        intent.putExtra("notifForm", true);
        intent.putExtra("uid", uid);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(
                CommonAppContext.sInstance, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(iid + "", iid + "", NotificationManager.IMPORTANCE_HIGH);
            Log.i("log", mChannel.toString());
            notificationManager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(CommonAppContext.sInstance)
                    .setChannelId(iid + "")
                    .setContentTitle(title)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setContentText(text)
                    .setSmallIcon(R.mipmap.ic_launcher).build();
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(CommonAppContext.sInstance)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentIntent(resultPendingIntent)
                    .setAutoCancel(true)
                    .setOngoing(true)
                    .setChannelId(iid + "");//无效
            notification = notificationBuilder.build();
        }
        notificationManager.notify(iid, notification);
    }

你可能感兴趣的:(sdk工具)