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);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章